【问题标题】:How can I bind a property to a view model in MAUI?如何在 MAUI 中将属性绑定到视图模型?
【发布时间】:2022-01-17 15:58:27
【问题描述】:

我正在尝试将属性绑定到视图模型。

我收到以下错误:

错误 XFC0009 找不到“ViewModel”的属性、BindableProperty 或事件,或者值和属性之间的类型不匹配。

public abstract class BaseTestView : ContentView
{
    public BaseVm ViewModel
    {
        get => (BaseVm)GetValue(ViewModelProperty);
        set => SetValue(ViewModelProperty, BindingContext = value);
    }
    public static BindableProperty ViewModelProperty { get; set; } = BindableProperty.Create(nameof(ViewModel), typeof(BaseVm), typeof(BaseTestView));
}
<v:BaseTestView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:vm="clr-namespace:MyProject.ViewModels"
    xmlns:v="clr-namespace:MyProject.Views"
    x:Class="MyProject.Views.ChildTestView"
    x:DataType="vm:ChildTestVm">
    <v:BaseTestView.Content>
      <StackLayout>
          <Label Text="{Binding Foo}" />
      </StackLayout>
  </v:BaseTestView.Content>
</v:BaseTestView>




public partial class ChildTestView : BaseTestView
{
    public ChildTestView() : base()
    {
        InitializeComponent();
    }
}

public class ChildTestVm : BaseVm
{
    public string Foo { get; set; }

    public ChildTestVm()
    {
        Title = "Test";
        Foo = "some stuff";
    }
}

public class HomeVm : BaseVm
{ 
    public ChildTestVm Tested { get; set; }
}
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:vm="clr-namespace:MyProject.ViewModels"
    xmlns:v="clr-namespace:MyProject.Views"
    x:Class="MyProject.Pages.HomePage"
    x:DataType="HomeVm">
    <ContentPage.Content>
      <StackLayout>
          <v:ChildTestView ViewModel="{Binding Tested}" />
          <!--             ^ Error here               /-->
      </StackLayout>
  </ContentPage.Content>
</ContentPage>




public partial class HomePage : ContentPage
{ 
}

知道这个错误的含义以及如何解决它吗?

【问题讨论】:

  • 您想要整个视图或特定堆栈布局的 Bindable 吗?
  • @SheikMydeenMuthu HomePage 有它的 HomeVm 视图模型,页面的一部分(ChildTestView)应该绑定到 Tested 提供的 ChildTestVm。
  • 我对毛伊岛不是很了解,但 setter 不应该只是: set => SetValue(ViewModelProperty, value);然后在 childview 中,您必须将 BindingContext 绑定到 xaml 中的 ViewModelProperty?
  • @T.Schwarz BindingContext = value 的值为 value,因此设置器同时设置 BindingContext 和 ViewModelProperty。

标签: c# xaml xamarin maui


【解决方案1】:

我尝试了一些实验,但无法弄清楚为什么会出现这种抱怨 - 我尝试的每个变体也都会出现这种错误。


相反,这样做:

首先,设置 ChildTestView 的BindingContext

<v:ChildTestView BindingContext="{Binding Tested}" />

该数据将 ChildTestView 绑定到来自 Tested 的 ChildTestVm。

如果您还需要访问 Vm 以获取后面的代码,请这样做:

ChildTestView.xaml.cs:

private ChildTestVm ViewModel => (ChildTestVm)BindingContext;

现在在 ChildTestView 的方法中,你可以使用ViewModel.Foo


注意:如果您动态更改Tested

如果您在任何地方都有代码在主页加载并可见之后执行Tested = ...,那么要使其工作需要Tested setter 执行OnPropertyChanged();(或其他MVVM 数据绑定机制)。这是通知 XAML 更改所必需的。

【讨论】:

  • 好的,这是一种解决方法,如果我找不到更好的我正在使用它。感谢您的尝试。
猜你喜欢
  • 2023-01-14
  • 1970-01-01
  • 2022-01-09
  • 2022-10-24
  • 2010-12-02
  • 2023-01-24
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多