Xamarin 已触发PropertyChanged,但未更新视图
我正在更改类构造函数中的标签,它工作正常,标签已更新(“0”)。当我点击一个按钮时,我也试图更新标签,但它不起作用(“X”)。我注意到标签值被更新,PropertyChanged被触发,但是视图没有改变Xamarin 已触发PropertyChanged,但未更新视图,xamarin,xamarin.ios,xamarin.forms,xamarin.android,Xamarin,Xamarin.ios,Xamarin.forms,Xamarin.android,我正在更改类构造函数中的标签,它工作正常,标签已更新(“0”)。当我点击一个按钮时,我也试图更新标签,但它不起作用(“X”)。我注意到标签值被更新,PropertyChanged被触发,但是视图没有改变 public class HomeViewModel : ViewModelBase { string playerA; public string PlayerA { get { return playerA;
public class HomeViewModel : ViewModelBase
{
string playerA;
public string PlayerA
{
get
{
return playerA;
}
set
{
playerA = value;
this.Notify("playerA");
}
}
public ICommand PlayerA_Plus_Command
{
get;
set;
}
public HomeViewModel()
{
this.PlayerA_Plus_Command = new Command(this.PlayerA_Plus);
this.PlayerA = "0";
}
public void PlayerA_Plus()
{
this.PlayerA = "X";
}
}
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void Notify(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
属性changedeventargs
中传递的参数名称错误。您正在使用“playerA”,但(public)属性的名称是“playerA”(大写“P”)。更改此通知(“playerA”)编码>至此通知(“PlayerA”)代码>或更好:
Notify(PlayerA的名字)代码>
通过将[CallerMemberName]
添加到Notify()
方法,可以完全避免传递参数的名称
受保护的无效通知([CallerMemberName]字符串propertyName=null)
这允许您只调用Notify()
,而不带参数,并且将自动使用已更改属性的名称 在这里写下你的xaml。我的意思是,如果标签没有显示,按钮和标签+将[string platerA;]更改为[string playerA=“!”;]!总之,您的绑定有一个问题。很高兴提及[CallerMemberName]
!