【发布时间】:2015-09-24 04:16:42
【问题描述】:
我已在论坛中搜索解决方案,但列出的解决方案都没有帮助。我假设我的实现已关闭,但我不明白是什么或为什么。
使用 Xamarin 表单,我试图在对象的数据发生更改时获取要更新的标签。
相关代码:
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 没有被触发。
我错过了什么?
【问题讨论】:
-
String 是一个关键字,可能会导致一些问题,您可以将其更改为“数据”吗?此外,您是否在页面上设置了绑定上下文?
-
您正在创建绑定但没有设置 BindingContext(至少它不在您发布的代码示例中)
-
Content 属性中的 SetBinding 与设置绑定上下文不同吗?
标签: c# xamarin xamarin.forms inotifypropertychanged propertychanged