【问题标题】:Label Text Not Updating From Bindable Property标签文本未从可绑定属性更新
【发布时间】:2016-12-13 09:16:47
【问题描述】:

我有一个非常简单的表单,我用它来试验 BindableProperty。这是表单的 XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MyBindableProperty"
             x:Class="MyBindableProperty.MainPage">
  <StackLayout>
    <local:MyLabel x:Name="BindingLabel" Text="{Binding Text}" MyText="{Binding Text}"
             VerticalOptions="Center"
             HorizontalOptions="Center" />
    <Entry x:Name="BindingEntry" Text="{Binding Text, Mode=TwoWay}"/>
    <Entry x:Name="BindingEntry2" Text="{Binding Text, Mode=TwoWay}"/>
    <Button x:Name="BindingButton" Text="Reset"/>
  </StackLayout>
</ContentPage>

这是背后的代码

    public partial class MainPage : ContentPage
    {
        public DataSourceClass DataSourceObject { get; set; }

        public MainPage()
        {
            DataSourceObject = new DataSourceClass { Text = "Test1" };
            BindingContext = DataSourceObject;

            InitializeComponent();
            BindingButton.Clicked += BindingButton_Clicked;
        }

        private void BindingButton_Clicked(object sender, EventArgs e)
        {
            var boundText = this.BindingLabel.Text;
            var boundMyText = this.BindingLabel.MyText;
            DataSourceObject.Text = "Test2";
        }
    }

最后,这是 XAML 中使用的自定义标签类 -

public class MyLabel : Label
{

    public string MyText
    {
        get
        {
            return (string)GetValue(MyTextProperty);
        }
        set
        {
            SetValue(MyTextProperty, value);
        }
    }

    public static readonly BindableProperty MyTextProperty = BindableProperty.Create(nameof(MyText), typeof(string), typeof(MyLabel), "Test", BindingMode.TwoWay, propertyChanged: MyTextChanged);

    public static void MyTextChanged(BindableObject bindable, object oldValue, object newValue)
    {
        ((MyLabel)bindable).TextChanged(newValue.ToString());
    }

    public void TextChanged(string newText)
    {
        Device.BeginInvokeOnMainThread(() => this.Text = newText);
    }
}

我遇到的问题是 当页面初始化 MyTextChanged 处理程序时触发,但不是在任何后续更改之后 单击 Reset 按钮时,DataSourceObject.Text 中的值已正确更新为 Entry 元素中的值 无论我如何尝试设置 BindingLabel 和 BindingEntry2 的值​​,它们都不会在页面加载后反映 DataSourceObject.Text 的值。

任何帮助将不胜感激。

【问题讨论】:

    标签: xaml xamarin.forms 2-way-object-databinding


    【解决方案1】:

    我偶然发现了this,所以我从这里更新了DataSourceClass

    public  class DataSourceClass
    {
        public string Text { get; set; }
    }
    

    到这里

    public class DataSourceClass : INotifyPropertyChanged
    {
        private string _text;
        public string Text
        {
            get
            {
                return _text;
            }
            set
            {
                _text = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Text"));
                }
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    }
    

    现在一切正常。

    我认为BindableProperty 是为了取代INotifyPropertyChanged

    【讨论】:

    • 你可以从 BindableObject developer.xamarin.com/api/type/Xamarin.Forms.BindableObject继承你的类
    • @FDB - 感谢您的评论。您提供的链接与解释我在答案中使用的 INotifyPropertyChanged 的​​链接相同。看来我只是误解了 BindableProperty 和 BindableObject 的用途。
    猜你喜欢
    • 1970-01-01
    • 2012-07-18
    • 2015-06-02
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 2011-12-18
    • 2014-04-28
    • 1970-01-01
    相关资源
    最近更新 更多