Xamarin表示,使用带有INT值的OnPropertyChanged

时间:2016-03-19 16:27:59

标签: c# xamarin xamarin.forms

我的MasterDetailPage上有一个按钮可以更改INT上的值(名为App.value1),具体取决于您点击的内容如下:

void click1 (object s, EventArgs a)
{
    if (App.value1 == 0) {

        App.value1 = App.value1 + 1;

    } else {
        App.value1 = 0;
    }
}

我想让这个点击功能立即更改我的StartPage上的值(另一个ContentPage)。所以我创建了一个看起来像这样的viewmodel,在那里我尝试使用当前值:

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged (string propertyName)
{
    var changed = PropertyChanged;
    if (changed != null) {

        PropertyChanged (this, new PropertyChangedEventArgs (propertyName));    
    }
}

public int currentValue {

        get {
            return App.value1;  
        }

        set {

            if (App.value1 == 0) {
                App.value1 = 0;

            } else {

                App.value1 = 1;
            }

        }   


    }

这是StartPage我希望INT的值立即更新,具体取决于您在MasterDetailView点击的内容。

public StartPage ()
{
        var ourView = new StartPageViewModel ();

        ourCurrentValue = ourView.currentValue;
}

protected async override void OnAppearing() 
{
    LoadData();
}


private async Task<List<Pin>> LoadData() //I work with pins here (not showing that code though as it is irrelavant.
{
    if (ourCurrentValue == 0) {
        System.Diagnostics.Debug.WriteLine ("Value is 0");
    }

    else {
        System.Diagnostics.Debug.WriteLine ("Value is 1");
    }
}

现在我只在日志中看到“值为0”。当我点击MasterDetailPage上的按钮时,没有任何更新。

更新代码:

public class StartPageViewModel : INotifyPropertyChanged
{
    private ICommand clickCommand;
    private int currentValue;

    public  event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged (string propertyName)
    {
        var changed = PropertyChanged;
        if (changed != null) 
        {
            PropertyChanged (this, new PropertyChangedEventArgs (propertyName));    
        }
    }


    public StartPageViewModel()
    {
        ClickCommand = new Command(() => CurrentValue = CurrentValue + 1);
    }

    public ICommand ClickCommand 
    {
        get { return clickCommand; }
        set
        {
            clickCommand = value;
            OnPropertyChanged("ClickCommand");
        }
    }
    public int CurrentValue 
    {
        get { return currentValue; }
        set
        {
            currentValue = value;
            OnPropertyChanged("CurrentValue");
        }
    }

}

和StartPage:

public StartPage ()
    {
        App.PropertyChanged += (sender, args) => OnPropertyChanged("currentValue"); // ERROR: `An object reference is requiered to access non-static member 'Xamarin.Forms.BindableObject.PropertyChanged`
    }

1 个答案:

答案 0 :(得分:1)

你可以继续这样的事情:

对该类中的App类和value1属性进行以下更改:

public static event PropertyChangedEventHandler PropertyChanged;

private static void OnPropertyChanged (string propertyName)
{
    var changed = PropertyChanged;
    if (changed != null) 
    {
        PropertyChanged (null, new PropertyChangedEventArgs    (propertyName));    
    }
}

private static int _value1;
public static int value1
{
    get { return _value1; }
    set 
    { 
        _value1 = value;
        OnPropertyChanged("value1");
    } 
}

然后将此行添加到StartPageViewModel构造函数中:

App.PropertyChanged += (sender, args) => OnPropertyChanged("currentValue");

在该代码中,您只是为了自己的目的而利用PropertyChanged(您甚至可以为此创建自己的事件)。 我的意思是StartPageViewModel订阅了PropertyChanged课程中的App个活动,因此会在value1更改时收到通知。当它实际发生时,它会调用自己的PropertyChanged来通知View currentValue更改。

但是,我认为更好的解决方案是在MasterDetailPageStartPage之间共享视图模型,因为使用全局状态会使您的解决方案难以理解:

public class SharedViewModel : INotifyPropertyChanged
{
    private ICommand clickCommand;
    private int currentValue;

    /* INotifyPropertyChanged implementation */

    public SharedViewModel()
    {
        ClickCommand = new Command(() => CurrentValue = CurrentValue + 1);
    }

    public ICommand ClickCommand 
    {
        get { return clickCommand; }
        set
        {
           clickCommand = value;
           OnPropertyChanged("ClickCommand");
        }
    }
    public int CurrentValue 
    {
        get { return currentValue; }
        set
        {
           currentValue = value;
           OnPropertyChanged("CurrentValue");
        }
    }
}

您需要在SharedViewModel以及MasterDetailPage

中使用StartPage的相同实例