绑定不适用于自定义 BindableProperty

Jay*_*one 5 c# xaml mvvm maui .net-maui

我在将值绑定到ContentView我所做的自定义时遇到问题。

我的MainPage用途CustomFrameViewContentView.

我的CustomFrameView有一根绳子BindableProperty。当我手动输入字符串时,这可以正常工作,但当我尝试使用绑定时,这会失败。我确信绑定应该有效,因为它在Label.

任何人都知道为什么它不起作用?

主页.xaml

        <StackLayout> 
    <!-- DOES work -->
          <Label Text="{Binding Name}"/>
    <!-- DOES work-->
          <controls:CustomFrameView TestName="Test456"/>
    <!-- DOES NOT work-->
          <controls:CustomFrameView TestName="{Binding Name}"/>
        </StackLayout>
Run Code Online (Sandbox Code Playgroud)

MainPage.xaml.cs

        <StackLayout> 
    <!-- DOES work -->
          <Label Text="{Binding Name}"/>
    <!-- DOES work-->
          <controls:CustomFrameView TestName="Test456"/>
    <!-- DOES NOT work-->
          <controls:CustomFrameView TestName="{Binding Name}"/>
        </StackLayout>
Run Code Online (Sandbox Code Playgroud)

MainPageViewModel.cs

    public MainPage()
    {
        InitializeComponent();

        BindingContext = new MainPageViewModel();
    }
Run Code Online (Sandbox Code Playgroud)

CustomFrameView.xaml.cs

    public partial class MainPageViewModel : ObservableObject
    {
        [ObservableProperty]
        private string name;
    
        public MainPageViewModel ()
        {
            Name = "Test123";
        }
    }
Run Code Online (Sandbox Code Playgroud)

CustomFrameViewModel.cs

    public partial class CustomFrameView : ContentView
    {
    public string TestName
        {
            get => (string)GetValue(TestNameProperty);
            set => SetValue(TestNameProperty, value);
        }
    
        public static readonly BindableProperty TestNameProperty =
            BindableProperty.Create(
                nameof(TestName),
                typeof(string),
                typeof(CustomFrameView),
                "",
                propertyChanged: SetTestNameProperty);
    
    
        private static void SetTestNameProperty(BindableObject bindable, object oldValue, object newValue)
        {     
            var vm = bindable.BindingContext as CustomFrameViewModel;
            vm.TestName = (string) newValue;
        }
    
        public CustomFrameView()
        {
            InitializeComponent();
            this.BindingContext = new CustomFrameViewModel();
        }
    }
Run Code Online (Sandbox Code Playgroud)

例子

Fre*_*Ali 3

实际上很简单,它不起作用的原因是您正在为控件本身设置不同的绑定上下文。它通常使用其父视图的 BC。您的控件是您的视图,它们没有单独的 ViewModel。您可以在这里温习 MVVM:https://learn.microsoft.com/en-us/dotnet/maui/xaml/fundamentals/mvvm

我建议您更改以下内容:

在您的自定义控件中,您可以执行以下操作:

public partial class CustomFrameView : ContentView
    {
    public string TestName
        {
            get => (string)GetValue(TestNameProperty);
            set => SetValue(TestNameProperty, value);
        }
    
        public static readonly BindableProperty TestNameProperty =
            BindableProperty.Create(
                nameof(TestName),
                typeof(string),
                typeof(CustomFrameView),
                string.Empty;
                );
    
    
        public CustomFrameView()
        {
            InitializeComponent();
        }
    }
Run Code Online (Sandbox Code Playgroud)

转储您的自定义控制虚拟机,然后看看这是否适合您。

另外,如果您使用毛伊岛,如果您想了解如何正确操作,可以查看我的自定义控件: https: //github.com/FreakyAli/MAUI.FreakyControls

祝你好运!