本文介绍了多个 BindingContext 在同一个 ContentPage 上,两个不同的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ContentPage,上面有两个 ContentView,我想将每个的绑定上下文设置为它们各自的 ViewModel(这是我对它们组合的一个庞大的 ViewModel 的首选解决方案)

I have a ContentPage with two ContentViews on it and i want to set the binding context for each of them each to their own respective ViewModel (this is my preferred soultion over one massive ViewModel for them combined)

主页

<?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 属性的注释时,应用程序会编译并开始运行,然后在加载 MainPage 时崩溃.

When I uncomment both bindingcontext attributes the App compiles, and starts to run and then crashes when loading the MainPage.

我是不是没有正确地实现这一点?还有其他方法吗?

Am i not implementing this correctly ?, is there another way to do it ?

推荐答案

答案

您可以使用其 BindingContext 属性为每个视图的绑定指定源,如下所示:

Answer

You can specify the source for each view's binding using its BindingContext property like so:

BindingContext="{Binding Source = {Your Binding Source}}"

以下示例应用展示了如何从同一 ContentPage 引用多个视图模型:https://github.com/brminnick/MultipleViewModelSample/

Here's a sample app that shows how to reference multiple view models from the same ContentPage: https://github.com/brminnick/MultipleViewModelSample/

<?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
        Grid.Row="0"
        Content="{Binding NavHeader}"
        HorizontalOptions="Start"
        VerticalOptions="Start"
        BindingContext="{Binding Source = {nav:NavigationHeaderViewModel}}"/>

    <ContentView
        Grid.Row="1"
        Content="{Binding DisplayedView}"
        HorizontalOptions="Center"
        VerticalOptions="Center"
        BindingContext="{Binding Source = {vm:MainPageViewModel}}"/>

  </Grid>

</ContentPage>

这篇关于多个 BindingContext 在同一个 ContentPage 上,两个不同的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 08:06