我有一个ContentPage,上面有两个ContentViews,我想为它们各自的ViewModel设置每一个的绑定上下文(这是我对他们的一个大型ViewModel的首选灵魂)
的MainPage
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="MVVMFramework.VVMs.Main.MainPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MVVMFramework"
xmlns:nav="clr-namespace:MVVMFramework.Navigation.NavigationHeader"
xmlns:vm="clr-namespace:MVVMFramework.VVMs.Main">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="200" />
</Grid.RowDefinitions>
//ContentView For Header
<ContentView Grid.Row="0"
HorizontalOptions="Start"
VerticalOptions="Start">
Content="{Binding NavHeader}"
<!--<ContentView.BindingContext>
<nav:NavigationHeaderViewModel />
</ContentView.BindingContext>-->
</ContentView>
//ContentView For Body of the app
<ContentView Grid.Row="1"
Content="{Binding DisplayedView}"
HorizontalOptions="Center"
VerticalOptions="Center">
<!--<ContentView.BindingContext>
<vm:MainPageViewModel />
</ContentView.BindingContext>-->
</ContentView>
</Grid>
</ContentPage>
当我取消注释两个bindingcontext属性时,App编译并开始运行,然后在加载MainPage时崩溃。
我没有正确实现这个吗?还有另一种方法吗?
答案 0 :(得分:5)
您可以使用K= 10
zi = 300
print(K)
print(K * 180)
print(K - 3241)
y=K
print(type(K))
print(type(y))
print(type(K[0]))
print(type(K[1]))
属性为每个视图的绑定指定源代码,如下所示:
BindingContext
BindingContext="{Binding Source = {Your Binding Source}}"
这是一个示例应用,展示了如何从同一ContentPage引用多个视图模型:https://github.com/brminnick/MultipleViewModelSample/