处理OnPropertyChanged [英] Handling OnPropertyChanged

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

问题描述

我不熟悉基于事件的编程。基本上,我仍然绊倒了。我正在尝试设置一些东西,但即使在教程中,我也不能围绕它。我想做的(用文字)如下:


  1. 我有一个属性更改的数据对象。我注意到这个属性的设置者,并想提出一个事件,该属性已经改变。


  2. 其他地方(完全不同的类),我想知道这个对象的属性已经改变了,并采取一些行动。


现在我确定这个这是一个很常见的情况,但是我的google-fu让我失望。我只是不了解 http://msdn.microsoft.com/en-us /library/ms743695.aspx



我有这个:

  public class ChattyClass {
private int someMember;

public event PropertyChangedEventHandler PropertyChanged;

public int SomeMember {
get {
return this.someMember;
}
set {
if(this.someMember!= value){
someMember = value;
//提高事件/消防处理程序。但是怎么样
}
}
}

public class NosyClass {
private List< ChattyClass> myChatters;

public void addChatter(ChattyClass chatter){
myChatters.add(chatter);
//开始听财产变更事件
}

private void listner(){
//当PropertyChangedEvent被调用$ b时,我想要调用
Console.WriteLine(嘿!嘿!听!我的列表中的一个喋喋不休的属性已经改变了!);
}
}

我该怎么办? p>

关于评论指向我的链接:



在示例中我看到:

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

我不明白:




  • 为什么不调用 PropertyChanged(这是新的PropertyChangedEventArgs(name)) li>
  • PropertyChanged在哪里被分配?

  • 作业的作用如何?


解决方案

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

  //创建OnPropertyChanged方法来引发事件
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if(handler!= null)
{
handler(this,new PropertyChangedEventArgs(name));
}
}

这个方法是什么,看看是否有事件处理程序分配与否(如果没有分配,你只是调用它,你会得到一个 NullReferenceException )。如果有一个分配,请调用此事件处理程序。提供的事件处理程序必须具有 PropertyChangedEventHandler 委托的签名。这个签名是:

  void MyMethod(object sender,PropertyChangedEventArgs e)
pre>

其中第一个参数必须是类型对象,并表示触发事件的对象,第二个参数包含此事件的参数。在这种情况下,您自己的类将触发事件,从而将 作为参数 sender 。第二个参数包含已更改的属性的名称。



现在可以对事件的触发做出反应,您必须为事件处理程序分配一个事件处理程序类。在这种情况下,您必须在您的 addChatter 方法中指定。除此之外,你必须首先定义你的处理程序。在 NosyClass 中,您必须添加一个方法来执行此操作,例如:

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

如您所见,此方法对应于我之前解释的签名。在第二个参数中,您可以找到哪些参数已更改的信息。最后要做的是添加事件处理程序。现在在您的 addChatter 方法中,您必须分配:

  public void AddChatter(ChattyClass chatter)
{
myChatters.Add(chatter);
//分配事件处理程序
chatter.PropertyChanged + = new PropertyChangedEventHandler(chatter_PropertyChanged);
}

我建议您阅读.NET / C#中的事件: a href =http://msdn.microsoft.com/en-us/library/awbftdfh> http://msdn.microsoft.com/en-us/library/awbftdfh 。我认为在阅读/学习之后,事情将会更加清楚。



您可以找到一个控制台应用程序这里在pastebin ,如果你想快速测试(只需复制/粘贴到一个新的控制台应用程序)。


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天全站免登陆