我已经在论坛上搜索了解决方案,但列出的解决方案都没有帮助。我认为我的实现有问题,但我不明白是什么或为什么。
使用Xamarin Forms,我正在尝试在对象数据更改时更新标签。
相关代码:
public new event PropertyChangedEventHandler PropertyChanged;
protected new virtual void OnPropertyChanged(string propertyName)
{
System.Diagnostics.Debug.WriteLine ("Before");
if (PropertyChanged != null)
{
System.Diagnostics.Debug.WriteLine ("Fired");
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
public string String {
set {
if (_data == value)
return;
_data = value;
OnPropertyChanged ( "String" ); }
get { return _data; }
}
public new View Content {
get {
label = new Label { Text = String };
label.SetBinding( Label.TextProperty, new Binding( "String" ) );
return label;}
}
基本上,“Before”被打印到控制台,但“Fired”没有被打印。这意味着 PropertyChanged 是空的,因此 PropertyChanged 没有被触发。
我错过了什么?