如何创建2遍视图?在谷歌上没有发现类似的东西
我有一个onePage.xaml.cs,它正在调用OneViewModel。在oneViewModel中,它将刷新页面,并且应该调用twoViewModel。最后,twoViewModel将调用一些随机视图
如何编辑view.xaml.cs文件以处理两个不同的ViewsModel?第一遍应绑定到一个ViewModel,第二遍应绑定两个ViewModel
onePage.xaml(查看(
<Label Text={Binding TestLabel} />
onePage.xaml.cs(查看(
public onePage()
{
InitializeComponent();
BindingContext = new oneViewModel();
}
oneViewModel.cs(视图模型1(
async oneViewModel()
{
TestLabel = "View Mode 1"
await Shell.Current.GoToAsync(Shell.Current.CurrentState);
}
twoViewModel.cs(视图模型2(
async twoViewModel()
{
TestLabel = "View Mode 2"
await Shell.Current.GoToAsync(RandomPage);
}
只需更新页面的BindingContext
<StackLayout Padding="0,100,0,0">
<Label Text="{Binding Description}" />
<Button Text="Change" Clicked="Button_Clicked" />
</StackLayout>
背后的代码
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
this.BindingContext = new VM("one");
}
void Button_Clicked(System.Object sender, System.EventArgs e)
{
this.BindingContext = new VM("two");
}
}
public class VM
{
public string Description { get; set; }
public VM(string desc)
{
this.Description = desc;
}
}