首页 > 解决方案 > 带有 OneWay 的 Xamarin 表单可绑定属性不起作用

问题描述

我想通过创建新的可绑定属性将 CustomLabel 绑定到 VM。

在 OneWay 模式下,第一个 VM 数据已正确更改 CustomLabel 的属性。但它从第二次开始就不起作用了。

尽管发生了 VM 事件,但 CustomView 的 Bindable 属性尚未触发其 PropertyChanged 事件。

不过,它在双向模式下可以正常工作。

测试了两天,一直在寻找原因,但我找不到它。

有人告诉我该怎么做吗?

// HomeViewModel.cs
public class HomeViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private string _customName = "-";
    
    public string CustomName
    {
        get
        {
            Debug.WriteLine("Get_CustomName");
            return _customName;
        }
        set
        {
            if (value != _customName)
            {
                Debug.WriteLine("Set_CustomName");
                _customName = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CustomName)));
            }
        }
    }
}

// MainPage.cs
public partial class MainPage : ContentPage
{
    HomeViewModel Vm = new HomeViewModel();

    public MainPage()
    {
        InitializeComponent();
        BindingContext = Vm;
    }

    void ButtonTrue_Clicked(object sender, EventArgs e)
    {
        Vm.CustomName = "True";
    }

    void ButtonFalse_Clicked(object sender, EventArgs e)
    {
        Vm.CustomName = "False";
    }
}

<!-- MainPage.xaml -->
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Ex_Binding"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="Ex_Binding.MainPage">
    <StackLayout Padding="50,0" VerticalOptions="Center">
        <StackLayout Orientation="Horizontal" HorizontalOptions="Center">
            <Label Text="Custom Result : " />
            <local:CustomLabel x:Name="lbCustom" MyText="{Binding CustomName}" HorizontalOptions="Center" />
        </StackLayout>
        <StackLayout Orientation="Horizontal">
            <Button Text="TRUE" BackgroundColor="LightBlue" HorizontalOptions="FillAndExpand" Clicked="ButtonTrue_Clicked" />
            <Button Text="FALSE" BackgroundColor="LightPink" HorizontalOptions="FillAndExpand" Clicked="ButtonFalse_Clicked" />
        </StackLayout>
    </StackLayout>
</ContentPage>

// CustomLabel.cs
public class CustomLabel : Label
{
    public static readonly BindableProperty MyTextProperty = BindableProperty.Create(nameof(MyText), typeof(string), typeof(CustomLabel), null, BindingMode.OneWay, propertyChanged: OnMyTextChanged);
    private static void OnMyTextChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var thisBindable = (CustomLabel)bindable;

        if (thisBindable != null)
        {
            thisBindable.MyText = (string)newValue;
        }
    }

    public string MyText
    {
        get => (string)GetValue(MyTextProperty);
        set
        {
            SetValue(MyTextProperty, value);
            Text = value;
        }
    }
}

标签: xamarinxamarin.forms

解决方案


原因 :

 thisBindable.MyText = (string)newValue;

因为您在MyText的值更改时设置了它的值。所以下次永远不会调用它(在 TwoWay 中,该方法将被多次调用)。

解决方案:

您应该直接设置Text in OnMyTextChanged

 private static void OnMyTextChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var thisBindable = (CustomLabel)bindable;

        if (thisBindable != null)
        {
            thisBindable.Text = (string)newValue;
        }
    }

    public string MyText
    {
        get => (string)GetValue(MyTextProperty);
        set
        {
            SetValue(MyTextProperty, value);
            //Text = value;
        }
    }

推荐阅读