【问题标题】:Xamarin Forms Bindable Property with OneWay is not working带有 OneWay 的 Xamarin 表单可绑定属性不起作用
【发布时间】:2020-05-09 10:01:48
【问题描述】:

我想通过创建新的可绑定属性将 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;
        }
    }
}

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    原因:

     thisBindable.MyText = (string)newValue;
    

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

    解决方案:

    你应该直接在OnMyTextChanged中设置Text

     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;
            }
        }
    

    【讨论】:

    • 不要忘记接受我的回答,这将帮助更多的人
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 2020-11-26
    • 1970-01-01
    相关资源
    最近更新 更多