C# PropertyChangedEventHandler 是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17736571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how does PropertyChangedEventHandler work?
提问by James Joshua Street
This is a really simple question, but I was wondering if someone could explain what the 4th line is actually doing? so the first line gives an event to the handler. I don't really know in what circumstances handler will return null or what the last line does.
这是一个非常简单的问题,但我想知道是否有人可以解释第 4 行实际上在做什么?所以第一行给处理程序一个事件。我真的不知道在什么情况下处理程序会返回 null 或者最后一行做了什么。
When you pass the handler your object and which property changed, what does it do with them?
当你将你的对象和哪个属性传递给处理程序时,它对它们有什么作用?
PropertyChangedEventHandler handler = PropertyChanged; //property changed is the event
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
I assume I used this to get this codebut I would like to understand what it is doing fully.
我假设我使用它来获取此代码,但我想完全了解它在做什么。
采纳答案by Scott Chamberlain
If you just did:
如果你只是这样做:
PropertyChanged(this, new PropertyChangedEventArgs(name))
you would get a NullReferenceException
if no one was subscribed to the event PropertyChanged
. To counteract this you add a null check:
NullReferenceException
如果没有人订阅该事件,您会得到一个PropertyChanged
。为了抵消这一点,您添加了一个空检查:
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name))
}
Now, if you are using multi-threading someone could unsubscribe between the null check and the calling of the event, so you could still get a NullReferenceException
. To handle that we copy the event handler to a temporary variable
现在,如果您使用多线程,有人可以在空检查和事件调用之间取消订阅,因此您仍然可以获得 NullReferenceException
. 为了解决这个问题,我们将事件处理程序复制到一个临时变量
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
Now if someone unsubscribes from the event our temporary variable handler
will still point to the old function and this code now has no way of throwing a NullReferenceException
.
现在,如果有人取消订阅该事件,我们的临时变量handler
仍将指向旧函数,并且此代码现在无法抛出NullReferenceException
.
Most often you will see people use the keyword var
instead, this makes it so you don't need to type in the full type of the temporary variable, this is the form you will see most often in code.
大多数情况下你会看到人们使用关键字var
来代替,这使得你不需要输入临时变量的完整类型,这是你在代码中最常看到的形式。
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
回答by H.B.
handler
can be null if no handlers are subscribedto the event, the fourth line raises the event for the property name given (which executes all subscribed handlers).
handler
可以为空,如果没有处理程序订阅的事件,第四行提出了在给定的属性名称的事件(其中执行所有订阅的处理程序)。
Usually the WPF framework will subscribe to PropertyChanged
when you use bindings, so it can update the binding once the bound property changes.
通常,WPF 框架会PropertyChanged
在您使用绑定时进行订阅,因此一旦绑定属性发生变化,它就可以更新绑定。
回答by poke
PropertyChanged
is the event that was declared like this, according to its definition in the interface:
PropertyChanged
是这样声明的事件,根据它在接口中的定义:
public event PropertyChangedEventHandler PropertyChanged;
Eventsthat are defined like that are actually a syntactic sugar for a list of event handlers you can add a delegate (that is a reference to a function) to by subscribing, or remove a delegate by unsubscribing.
像这样定义的事件实际上是事件处理程序列表的语法糖,您可以通过订阅添加委托(即对函数的引用),或通过取消订阅删除委托。
Now, when you call an event, i.e. PropertyChanged(...)
, then what happens internally is that every delegate in that internal list is called separately with the parameters. This will tell all the subscribers of your event that the event happened.
现在,当您调用一个事件时,即PropertyChanged(...)
,内部发生的事情是该内部列表中的每个委托都使用参数单独调用。这将告诉您事件的所有订阅者该事件发生了。
Now, the reason for the whole thing with the handler
variable is, that PropertyChanged
can be null. If nothing subscribed to it, then calling the event (or rather the event handler list) would not work, so this is just a way to ensure that you can actually execute the handler.
现在,handler
变量的整个事情的原因是,它PropertyChanged
可以为空。如果没有任何订阅它,那么调用事件(或者更确切地说是事件处理程序列表)将不起作用,因此这只是确保您可以实际执行处理程序的一种方法。