当我尝试在XAML中绑定标签时,我现在没有文本。由于某种原因,它似乎在我的viewmodel中找不到我的字符串Forename
的“setvalue”。当我的Forename
尝试设置不同的值时,我将其写入日志中,但它找不到其中任何一个。不是if,不是if else而不是else。
(myInt
是int
,其值为0
,1
或2
。默认情况下为0
。)< / p>
我有一个带有此代码的viewmodel:
public class MenuViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var changed = PropertyChanged;
if (changed != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
string info;
public string Forename
{
get { return info; }
set
{
if (myInt == 0)
{
info = "TextOne";
OnPropertyChanged("Forename");
}
else if (myInt == 1)
{
info = "TextTwo";
OnPropertyChanged("Forename");
}
else
{
info = "TextThree";
OnPropertyChanged("Forename");
}
// When I try to write out these if, else if, else in the log
// I get nothing. So it does not seem to reach them.
}
}
}
这就是我在我的内容页面+ XAML上收到它的方式:
public MenuPage()
{
InitializeComponent();
BindingContext = new MenuViewModel();
}
使用XAML:
<Label Text = "{Binding Forename}" />