gpt4 book ai didi

c# - Josh Smith 的 MVVM 示例应用程序中关于 EventHandler 的问题

转载 作者:行者123 更新时间:2023-11-30 14:41:48 24 4
gpt4 key购买 nike

以下代码来自 Josh Smith 的 MVVM 示例:

/// <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);
}

注释行是我添加的。我的问题是注释行会和未注释行做同样的事情吗?那么为什么要创建另一个 EventHandler 引用呢?还是我在这里遗漏了什么?谢谢

最佳答案

Tanmoy 是对的。这样做是为了防止在您的“if”之后但在您的“RequestClose()”之前在其他线程中更改 RequestClose 的可能性(例如,更改为 null)。

关于c# - Josh Smith 的 MVVM 示例应用程序中关于 EventHandler 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3757234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com