如何将模型更改通知给MVVM中的ViewModel? [英] How the Model changes could be notified to ViewModel in MVVM?

查看:143
本文介绍了如何将模型更改通知给MVVM中的ViewModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像我们通过INotifyPropertyChanged通知View有关ViewModel成员发生的更改一样,将模型中的更改通知给ViewModel的常规方法是什么?

解决方案

< blockquote>

您也可以为模型实现 INotifyPropertyChanged 界面。


感谢Shmuel Zang的回复。



我在模型中实现了INotifyPropertyChanged,并在我的ViewModel中订阅了模型的PropertyChanged事件。有用。



我们知道通过应用[CallerMemberName]属性(在OnPropertyChanged()中使用),我们避免重命名重构不会更改字符串值问题。我想知道有没有像[CallerMemberName]属性这样的机制来避免同样的问题(重命名重构不会改变字符串值),这可能发生在我的ViewModel中的myModel_PropertyChanged()事件处理程序中?

(代码中的exaclty:if(e.PropertyName ==MyFirstName))



这是我的代码:



 内部  class 模型:INotifyPropertyChanged 
{
public Model()
{
}

string _FirstName = Shahir;
public string FirstName
{
get { return _FirstName; }
set
{
_FirstName = value ;
OnPropertyChanged();
}
}

public event PropertyChangedEventHandler PropertyChanged ;

private void OnPropertyChanged([CallerMemberName] string propertyName =
{
if (PropertyChanged!= null
{
PropertyChanged( new PropertyChangedEventArgs(propertyName));
}
}
}



  internal   class  MyViewModel:INotifyPropertyChanged 
{
private 模型myModel;

public MyViewModel(模型型号)
{
this .myModel = model;
myModel.PropertyChanged + = myModel_PropertyChanged;
}


public event PropertyChangedEventHandler PropertyChanged;

private void OnPropertyChanged([CallerMemberName] string propertyName =
{
if (PropertyChanged!= null
{
PropertyChanged( new PropertyChangedEventArgs(propertyName));
}
}


string _FirstName;
public string MyFirstName
{
get { return myModel.FirstName; }
set
{
_FirstName = value ;
OnPropertyChanged();
}
}

私有 void myModel_PropertyChanged( object sender,System.ComponentModel.PropertyChangedEventArgs e)
{
if (e .PropertyName == FirstName
{
MyFirstName = myModel.FirstName ;
}
}
}


Like we notify the View about the changes happened to the ViewModel members with INotifyPropertyChanged, what is the normal way to notify the changes in the Model to the ViewModel?

解决方案

You can implement the INotifyPropertyChanged interface also for the model.


Thanks Shmuel Zang for your reply.

I have implemented the INotifyPropertyChanged in the model and subscribed the PropertyChanged event of the model in my ViewModel. It works.

As we know that by applying [CallerMemberName] attribute (used in OnPropertyChanged()), we avoid the "Rename Refactoring doesn't change the String values" problem. I would like to know that is there any mechanism like [CallerMemberName] attribute to avoid the same problem ("Rename Refactoring doesn't change the String values") which may happen in the myModel_PropertyChanged() event handler in my ViewModel?
(exaclty here in the code: if (e.PropertyName == "MyFirstName"))

Here is my code:

internal class Model:INotifyPropertyChanged
    {
        public Model()
        {
        }

        string _FirstName = "Shahir";
        public string FirstName
        {
            get { return _FirstName; }
            set
            {
                _FirstName = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            if(PropertyChanged!=null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }


internal class MyViewModel:INotifyPropertyChanged
    {
        private Model myModel;

        public MyViewModel(Model model)
        {
            this.myModel = model;
            myModel.PropertyChanged += myModel_PropertyChanged;
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }


        string _FirstName;
        public string MyFirstName
        {
            get { return myModel.FirstName; }
            set
            {
                _FirstName = value;
                OnPropertyChanged();
            }
        }

        private void myModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "FirstName")
            {
                MyFirstName = myModel.FirstName;
            }
        }
    }


这篇关于如何将模型更改通知给MVVM中的ViewModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆