【问题标题】:how to pass multiple labels through ContentPage如何通过 ContentPage 传递多个标签
【发布时间】:2021-04-19 01:50:46
【问题描述】:

我正在寻求帮助设置后面的代码以使用下一步按钮传递多个标签。基本上我想在打开页面时设置一个标签,按下一步按钮并用一个新标签替换当前标签(不设置新的内容页面)。我是在 Xamarin.Forms 工作的初学者,我并不真正了解数据绑定过程......如果有人有一个很好的参考(除了 Microsoft 网站)也会有所帮助。很确定下面的代码不会做任何事情......提前谢谢:)

这是内容页面:

<ContentPage.Content>
    <StackLayout>
        <Label Text="{Binding TitleText}" />
        <ScrollView VerticalOptions="FillAndExpand">
            <StackLayout>
                <Label Text="{Binding EngText}" />

                <Label Text="{Binding ItText}" />

            </StackLayout>
        </ScrollView>

这是我为后面的代码开始的:

''''''

namespace MVVM2
{
public partial class MainPage : ContentPage
{
    List<MainPage> Contacts { get; set; }
    int ndx = 0;

    public string TitleText { get; set; }
    public string EngText { get; set; }
    public string ItText { get; set; }

    public MainPage()
    {
        InitializeComponent();

        Contacts = new List<MainPage>();

        // repeat this for as many contacts as you need
        Contacts.Add(new MainPage
        {
            TitleText = "Title1",
            EngText = "EngText1",
            ItText = "ItText1"
        });

        Contacts.Add(new MainPage
        {
            TitleText = "Title2",
            EngText = "EngText2",
            ItText = "ItText2"
        });

        Contacts.Add(new MainPage
        {
            TitleText = "Title3",
            EngText = "EngText3",
            ItText = "ItText3"
        });

        // display the first contact
        BindingContext = Contacts[ndx];
    }

    private void OnNavigateButtonClicked(object sender, EventArgs e)
    {
        // increment your index
        ndx++;

        // check that we haven't gone too far
        if (ndx < Contacts.Count)
        {
            BindingContext = Contacts[ndx]; 
        }
    }
  }
}

【问题讨论】:

  • 我们先梳理一下模型类。联系是为了什么?是否还有更多文本,例如 MathText、PhyText?
  • 这只是三个标签和 EngText 和 ItText 之间的图像。联系方式是我借鉴的示例的一部分,将根据我正在完成的部分更改为更合适的作品,例如 Longsword 或 Dagger。

标签: xamarin.forms data-binding


【解决方案1】:

如果您只想在单击按钮时显示不同的文本,则无需导航到新页面

首先,创建一个List 来保存您的按钮和一个变量来跟踪显示哪个按钮。这两行应该在你的类的主体中,而不是在任何特定的方法中

List<Contact> contacts { get; set; }
int ndx = 0;

然后在你的构造函数中设置你的数据

public MainPage()
{
    InitializeComponent();
 
    contacts = new List<Contact>();

    // repeat this for as many contacts as you need
    contacts.Add(new Contact { 
        TitleText = "Title1",
        EngText = "EngText1",
        ItText = "ItText1"});
   
    // display the first contact
    BindingContext = contacts[ndx];
}

最后,处理按钮点击

async void OnNavigateButtonClicked(object sender, EventArgs e)
{
   // increment your index
   ndx++;

   // check that we haven't gone too far
   if (ndx < contacts.Count) {
     BindingContext = contacts[ndx];
   }
}

【讨论】:

  • 嗨,Jason,我的绑定似乎没有从代码隐藏中提取标签。我错过了什么吗?
  • 尝试移动 Contact 类,使其不会嵌套在您的页面类中
  • 嗨,Jason,我发现了问题……页面无法初始化……有没有办法向您展示我所做的事情?
  • 是的,编辑您的帖子以包含任何其他信息或代码
  • 我不知道您当前要解决的问题是什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-21
  • 2021-10-25
  • 1970-01-01
相关资源
最近更新 更多