处理OnPropertyChanged [英] Handling OnPropertyChanged

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

问题描述

我不是在基于事件的编程精通。基本上,我仍然磕磕绊绊与它周围。我想要得到的东西成立,但即使有教程,我不能换我的头周围。我想这样做(字)如下:




  1. 我有一个数据对象,其中一个属性更改。我注意到这个在setter方法​​,并希望提高一个事件,属性已更改。


  2. 在其他地方(在完全不同的类),我要知道,这个对象的属性发生了变化,并采取了一些行动。




现在我敢肯定,这是一种常见的足够的场景,但我的谷歌福是让我失望。我只是不理解 http://msdn.microsoft.com/en-us 。/library/ms743695.aspx



我有这样的:

 公共类ChattyClass {
私人诠释someMember;

公共事件PropertyChangedEventHandler的PropertyChanged;

公众诠释SomeMember {
获得{
返回this.someMember;
}
集合{
如果(this.someMember =价值!){
someMember =价值;
//引发事件/火处理程序。但如何?
}
}
}

公共类NosyClass {
私有列表< ChattyClass> myChatters;

公共无效addChatter(ChattyClass唠){
myChatters.add(唠叨);
//开始监听属性更改事件
}

私人无效听者(){
//我想这被称为当PropertyChangedEvent被称为
Console.WriteLine(嘿,嘿,听我的列表中喋喋不休的属性发生变化!!!);
}
}



我该怎么办,以这样组装起来?



关于评论指着我回链接:



在例子中,我看到:

 保护无效OnPropertyChanged(字符串名称)
{
PropertyChangedEventHandler处理器=的PropertyChanged;
如果(处理!= NULL)
{
处理器(这一点,新PropertyChangedEventArgs(名));
}
}



什么我不理解:




  • 为什么不就是叫的PropertyChanged(这一点,新PropertyCHangedEventArgs(名))

  • 哪里的PropertyChanged得到分配?

  • 什么是分配样子?


< DIV CLASS =h2_lin>解决方案

您必须触发事件。在MSDN上的例子,他们做了一个受保护的方法 OnPropertyChanged 来处理这个容易(并避免重复代码)。

  //创建OnPropertyChanged方法引发事件
保护无效OnPropertyChanged(字符串名称)
{
PropertyChangedEventHandler处理器=的PropertyChanged;
如果(处理!= NULL)
{
处理器(这一点,新PropertyChangedEventArgs(名));
}
}



这是什么方法呢,是看是否存在事件处理程序,转让或不(如果它未分配,你只需要调用它,你会得到一个的NullReferenceException )。如果有一个分配的,称此事件处理程序。提供的事件处理程序,必须具有 PropertyChangedEventHandler 委托的签名。这个签名是:



 无效的MyMethod(对象发件人,PropertyChangedEventArgs E)

当第一个参数是类型的对象,并表示触发事件的对象,第二个参数包含此事件的参数。在这种情况下,自己的类触发事件,从而给这个为参数发件人。第二个参数包含已更改的属性的名称。



现在,能够在事件的触发反应,你必须指定一个事件处理程序类。在这种情况下,你必须在你的 addChatter 方法分配此。除此之外,你必须首先定义您的处理程序。在你的 NosyClass 你必须添加要做到这一点的方法,例如:

 私人无效chatter_PropertyChanged(对象发件人,PropertyChangedEventArgs E)
{
Console.WriteLine(A属性发生变化:+ e.PropertyName);
}



正如你所看到的,这种方法相当于我以前解释的签名。在第二个参数,你就可以找到它的参数已经改变的信息。最后一件事要做,就是添加事件处理程序。现在,在你的 addChatter 方法,你必须指定这个:

 公共无效AddChatter(ChattyClass唠)
{
myChatters.Add(唠叨);
//将事件处理程序
chatter.PropertyChanged + =新PropertyChangedEventHandler(chatter_PropertyChanged);
}



我建议你读一些有关使用.NET / C#事件:< A HREF =htt​​p://msdn.microsoft.com/en-us/library/awbftdfh> http://msdn.microsoft.com/en-us/library/awbftdfh 。我觉得看完/学习这一点,事情会更清楚你。



您可以找到一个控制台应用程序的在这里引擎收录如果您想快速测试它(只是复制/粘贴到一个新的控制台应用程序)。


I'm not well versed in event-based programming. Basically, I'm still stumbling around with it. I'm trying to get something set up, but even with the tutorials, I can't wrap my head around it. What I would like to do (in words) is the following:

  1. I have a dataobject where a property changes. I notice this in the setter of the property, and want to raise an event that the property has changed.

  2. Elsewhere (in a different class entirely), I want to know that the property on this object has changed, and take some action.

Now I'm sure this is a common enough scenario, but my google-fu is letting me down. I'm simply not understanding http://msdn.microsoft.com/en-us/library/ms743695.aspx.

I have this:

public class ChattyClass {
  private int someMember;

  public event PropertyChangedEventHandler PropertyChanged;

  public int SomeMember {
    get {
      return this.someMember;
    }
    set {
      if (this.someMember != value){
        someMember = value;
        // Raise event/fire handlers. But how?
      }
   }
}

public class NosyClass{
  private List<ChattyClass> myChatters;

  public void addChatter(ChattyClass chatter){
    myChatters.add(chatter);
    // Start listening to property changed events
  }

  private void listner(){
    // I want this to be called when the PropertyChangedEvent is called
    Console.WriteLine("Hey! Hey! Listen! A property of a chatter in my list has changed!");
  }
}

What do I do to wire this up?

Concerning the comment pointing me back to the link:

In the example I see:

protected void OnPropertyChanged(string name)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(name));
    }
}

What I'm not understanding:

  • Why isn't this just calling PropertyChanged(this, new PropertyCHangedEventArgs(name))
  • Where does PropertyChanged get assigned?
  • What does the assignment look like?

解决方案

You have to fire the event. In the example on MSDN, they made a protected method OnPropertyChanged to handle this easier (and to avoid duplicate code).

// Create the OnPropertyChanged method to raise the event 
protected void OnPropertyChanged(string name)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(name));
    }
}

What this method does, is look whether there is an event handler assigned or not (if it is not assigned and you just call it, you'll get a NullReferenceException). If there is one assigned, call this event handler. The event handler provided, has to have the signature of the PropertyChangedEventHandler delegate. This signature is:

void MyMethod(object sender, PropertyChangedEventArgs e)

Where the first parameter has to be of the type object and represents the object that fires the event, and the second parameter contains the arguments of this event. In this case, your own class fires the event and thus give this as parameter sender. The second parameter contains the name of the property that has changed.

Now to be able to react upon the firing of the event, you have to assign an event handler to the class. In this case, you'll have to assign this in your addChatter method. Apart from that, you'll have to first define your handler. In your NosyClass you'll have to add a method to do this, for example:

private void chatter_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    Console.WriteLine("A property has changed: " + e.PropertyName);
}

As you can see, this method corresponds to the signature I explained before. In the second parameter, you'll be able to find the information of which parameter has been changed. Last thing to do, is add the event handler. Now in your addChatter method, you'll have to assign this:

public void AddChatter(ChattyClass chatter)
{
    myChatters.Add(chatter);
    // Assign the event handler
    chatter.PropertyChanged += new PropertyChangedEventHandler(chatter_PropertyChanged);
}

I would suggest you to read something about events in .NET / C#: http://msdn.microsoft.com/en-us/library/awbftdfh . I think after reading/learning this, things will be more clear to you.

You can find a console application here on pastebin if you would like to test it quickly (just copy/paste into a new console application).

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

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