Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin 已触发PropertyChanged,但未更新视图_Xamarin_Xamarin.ios_Xamarin.forms_Xamarin.android - Fatal编程技术网

Xamarin 已触发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;

我正在更改类构造函数中的标签,它工作正常,标签已更新(“0”)。当我点击一个按钮时,我也试图更新标签,但它不起作用(“X”)。我注意到标签值被更新,PropertyChanged被触发,但是视图没有改变

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]