【发布时间】: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