c# – 如何强制取消可以取消的活动?
发布时间:2020-12-16 01:29:55 所属栏目:百科 来源:网络整理
导读:请帮助我实现一个事件,处理程序可以取消它. public class BuildStartEventArgs : EventArgs{ public bool Cancel { get; set; }}class Foo{ public event EventHandlerBuildStartEventArgs BuildStart; private void Bar() { // build started OnBuildStart(
请帮助我实现一个事件,处理程序可以取消它.
public class BuildStartEventArgs : EventArgs { public bool Cancel { get; set; } } class Foo { public event EventHandler<BuildStartEventArgs> BuildStart; private void Bar() { // build started OnBuildStart(new BuildStartEventArgs()); // how to catch cancellation? } private void OnBuildStart(BuildStartEventArgs e) { if (this.BuildStart != null) { this.BuildStart(this,e); } } } 解决方法
您需要修改此代码:
private void Bar() { // build started OnBuildStart(new BuildStartEventArgs()); // how to catch cancellation? } 这样的事情: private void Bar() { var e = new BuildStartEventArgs(); OnBuildStart(e); if (!e.Cancel) { // Do build } } .NET中的类具有引用语义,因此您可以看到对事件参数所做的任何更改引用. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |