标签文本未从可绑定属性更新

时间:2016-12-13 09:16:48

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

我有一个非常简单的表单,我用它来试验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处理程序时,但不会在任何后续更改之后触发 单击“重置”按钮时,DataSourceObject.Text中的值将使用Entry元素中的值正确更新 无论我如何设置BindingLabel和BindingEntry2的值,它们都不会在页面加载后反映DataSourceObject.Text的值。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我偶然发现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