本文介绍了问题的事件处理程序从约什 - 史密斯的MVVM示例应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码是由约什 - 史密斯的MVVM示例:

The following code is from the MVVM sample by Josh Smith:

/// <summary>
/// Raised when this workspace should be removed from the UI.
/// </summary>
public event EventHandler RequestClose;

void OnRequestClose()
{
    //if (RequestClose != null)
    //        RequestClose(this, EventArgs.Empty);
    EventHandler handler = this.RequestClose;
    if (handler != null)
        handler(this, EventArgs.Empty);
 }



注释行是我的加法。我的问题是注释行会做同样的事情注释的行权?那么,为什么创建另一个事件处理程序的参考?还是我失去了一些东西在这里?谢谢

The commented lines are my addition. My question is the commented lines would do the same thing as the uncommented lines right? So why create another EventHandler reference? Or am I missing something here? Thanks

推荐答案

Tanmoy是正确的。这样做是为了防止被改变RequestClose的可能性(为空,例如)在其他线程后,您的如果,但你的RequestClose()之前。

Tanmoy is right. This is done to prevent possibility of RequestClose being changed (to null, for example) in other thread after your "if" but before your "RequestClose()".

这篇关于问题的事件处理程序从约什 - 史密斯的MVVM示例应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 05:10