MVVM和INotifyPropertyChanged的问题 [英] MVVM and INotifyPropertyChanged Issue

查看:102
本文介绍了MVVM和INotifyPropertyChanged的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVVM设计的一个大问题。我试图抓住我的内心嵌套对象的每一个PropertyChanged的,包括futhermore propertchanged其嵌套的对象,我的视图模型里面,但我不知道该怎么做。

I have a big problem with MVVM design. I am trying to catch every PropertyChanged of my inner nested objects, including futhermore propertchanged of their nested objects, inside my ViewModel but I dont know how to do it.

下面是我的结构:

class MyVM
{

  public MyVM()
  {
    this.SomeData = new SomeData();
    this.SomeData.NestedObj = new MyNestedDat();
    this.SomeData.Str = "This tiggers propertychanged inside MyDat class";
    // this triggers propertychanged event inside MyNestedDat class
    this.SomeData.NestedObj.Num = 123;
  }

  // and here should be a method where i catch all possibe propertychanges from my nested objets and their nested objets, how do i do that?

  public MyDat SomeData
  {
    get;
    set;
  }

}

class MyDat : INotifyPropertyChanged
{
  private string str;
  public string Str;
  {
    get { return this.str;}
    set
    {
      this.str = value;
      this.PropertyChanged(this, "Str");
    }
  }


  publicMyNestedDat NestedObj
  {
   get;
   set;
  }
}

class MyNestedDat : INotifyPropertyChanged
{
  private int num;
  public int Num
  {
    get{ return this.num;}
    set
    {
      this.num = value;
      this.PropertyChanged(this, "Num");
    }
  }
}



我如何得到这工作?我真的一无所知从哪里开始。

How do i get this to work? I am really clueless where to start.

MyNestedDat类引发的PropertyChanged,MyDat类引发的PropertyChanged,我想赶上他们我所有的视图模型中。我该怎么做?

MyNestedDat class throws PropertyChanged, MyDat class throws propertychanged and i want to catch them all inside my viewmodel. How can i do that?

推荐答案

在我看来有一些概念性的东西错了你的要求。试想一下,你得到您的方案作品(你是满意)的解决方案,并考虑以下内容:

In my opinion there are a few conceptual things wrong with what you are asking. Just imagine you get a solution that works for your scenario (that you are happy with) and consider the following:


  • 如果另一层会发生什么加入?你还期望它的工作一样吗?

  • 如果属性改变传播( viewModel1.propA 通知 viewModel2.PropA )?

  • 如果属性更改转化( viewModel1.SomeProp 通知 ViewModel2.AnotherProp )?

  • 是性能的关注?这将如何执行,如果你需要传播的属性通过多层次变化事件?

  • What happens if another layer is added? do you still expect it to work the same?
  • Should property changes be propagated (viewModel1.propA notifies viewModel2.PropA)?
  • Should property changes be transformed (viewModel1.SomeProp notifies ViewModel2.AnotherProp)?
  • Is performance a concern? how will this perform if you need to propagate the property changed events through many levels?

这应该被提高了警钟,目前的做法是不是踩在正确的道路。

This should be raising alarm bells that the current approach is not the right path to tread.

您需要的是一种方法来提供一个松散耦合的方式您的ViewModels之间的沟通,让您的ViewModels甚至不需要知道关于彼此的存在。这样做的好处是,这也将在其他情况下的工作不只是属性更改。

What you need is a way to provide communication between your viewModels in a loosely coupled way so that you viewModels do not even need to know about each others existence. The beauty of this is that this will also work in other situations not just for property changes.

有关你的财产情况变化事件,人们视图模型想知道的当事情发生的(这可能是比属性更改事件以外的东西,记住)。这意味着其他视图模型需要说:嘿,一个属性更改的一些方法(或我的状态已经改变了,这个数据库调用完成等等)。

For your case of property changed events, one viewModel wants to know when something happens (it could be something other than a property changed event, remember). This means the other viewModel needs some way of saying "Hey, a property has changed" (or "My state has changed", "That database call has finished" etc).

现在在C#中,你可以提供这种提供功能....但,现在你的对象知道对方这让你与你之前有同样的问题的事件。

Now in C# you can provide events which provide this feature....except, now your objects know about each other which leaves you with the same problem you had before.

要解决这个问题,您需要另一个对象,调解员(可以称之为使者在这个例子中),其唯一目的是处理对象,以便之间传递消息他们可以住对方的无知。

To overcome this problem you need another object, a mediator (lets call it Messenger in this example), whose sole purpose is to handle the message passing between the objects so that they can live in ignorance of each other.

总的想法是这样的。在提供通知视图模型,你可以做这样的事情:

The general idea is this. In the viewModel that provides notifications you might do something like this:

public string MyProp
{
    get { return _myProp; }
    set
    {
        _mProp = value;
        OnPropertyChanged("MyProp");
        Messenger.PostMessage(new VMChangedMessage { ViewModel = this, PropertyName = "MyProp" });
    }
}

和在视图模型是有兴趣的事件,你可能做这样的事情:

And in the viewModel that is interested in the event you might do something like this:

public class ViewModel2
{
    public ViewModel2()
    {
        Messenger.Subscribe<VMChangedMessage>(handleMessage);
    }

    private void handleMessage(VMChangedMessage msg)
    {
        // Do something with the information here...
    }
}

注意两个的ViewModels从来没有相互引用。它们现在松耦合

Notice that the two viewModels never reference each other. They are now loosely-coupled.

有已经提供了一些预先存在实现的,这是不难创建自己(信使基本保持列表那有兴趣在一定的消息,并遍历列表时,它需要通知有关当事人)的对象。有迹象表明,可以分别实施了一些东西(一些实现只是传递字符串的邮件周围,而不是封装在对象中的信息,以及一些处理观察员的清理自动)。

There are a number of pre-existing implementations already available and it isn't difficult to create your own (the messenger basically keeps a list of objects that are interested in a certain message and iterates the list when it needs to notify the interested parties). There are a few things that can be implemented differently (some implementations just pass string messages around rather than encapsulating the information in objects, and some handle the clean-up of observers automatically).

我会建议使用约什 - 史密斯(优秀) MVVM基金会其中包括一个信使类。这也是开源的,所以你可以看到它是如何工作的。

I would recommend using Josh Smiths (excellent) MVVM Foundation which includes a messenger class. It's also open source so you can see how it works.

这篇关于MVVM和INotifyPropertyChanged的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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