【发布时间】:2016-03-07 21:31:55
【问题描述】:
尝试通过字符串将文本绑定到我的 xaml 并使用 propertychanged 函数根据 int 的值更改文本。 这是我的代码:
XAML:
<Label Text = "{Binding Forename}" />
代码:
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged (string propertyName)
{
var changed = PropertyChanged;
if (changed != null) {
PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
}
}
string info;
int myInt = 0;
public string Forename {
get {
return info;
}
set {
if (myInt == 0) {
info = value;
OnPropertyChanged ("TextOne");
}
else if (myInt == 1)
{
OnPropertyChanged ("TextTwo");
}
else
{
OnPropertyChanged ("TextThree");
}
}
}
我现在没有收到任何文字。
【问题讨论】:
-
为什么使用“TextOne”、“TextTwo”和“TextThree”作为属性名称?看起来该属性的名称是
Forename。 -
啊哈,我以为我在那里为我的标签制作文字?
-
否... 将属性名称放在那里。新的文本值应该只是属性值,因此在 setter 方法中,您需要始终将
info设置为您想要的属性值。然后启动 OnPropertyChanged。它使用属性名称来知道哪个属性发生了变化。
标签: c# xamarin xamarin.forms