Xamarin在模型更改时更新ViewModel值

时间:2019-04-24 05:17:26

标签: c# xamarin mvvm viewmodel

假设有一个模型:

public class MyCustomModel 
{
    public bool parameter { get; set; }
    ....
}

还有一些ViewModel:

public class MainViewModel : INotifyPropertyChanged
{
    private MyCustomModel model;

    private bool _missingValue = true;
    public bool MissingValue
    {
       get => _missingValue;
       set
       {
           if (_missingValue == value) return;
           _missingValue = value;
           OnPropertyChanged(nameof(MissingValue));
       }
    }
    public MainViewModel(MyCustomModel model)
    {
        this.model = model;
    }
}

public class AdvancedViewModel : INotifyPropertyChanged 
{
    private MyCustomModel model;

    private bool _missingValue = true;
        public bool MissingValue
        {
            get => _missingValue;
            set
            {
                if (_missingValue == value) return;
                _missingValue = value;
                model.parameter = _missingValue;
                OnPropertyChanged(nameof(MissingValue));
            }
        }

    public AdvancedViewModel(MyCustomModel model) 
    {
        this.model = model;
    }
}

当AdvancedViewModel中的值更改时(需要调用设置器),我需要更改MainViewModel中的值。那么如何正确地做到这一点呢?有什么建议么?预先感谢。

0 个答案:

没有答案