小编Jun*_*Jon的帖子

多个BindingContext在同一ContentPage上,有两个不同的视图?

我有一个带有两个ContentViews的ContentPage,并且我想将它们各自的绑定上下文设置为各自的各自的ViewModel(这是我首选的合并形式,而不是一个庞大的ViewModel)

主页

<?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>
Run Code Online (Sandbox Code Playgroud)

当我取消注释这两个bindingcontext属性时,应用程序会编译,并开始运行,然后在加载MainPage时崩溃。

我执行不正确吗?还有另一种方法吗?

xaml xamarin.ios xamarin.android xamarin xamarin.forms

1
推荐指数
1
解决办法
2413
查看次数