.NET Framework、WPF:取消函数引发事件

.NET Framework, WPF: Cancel Function Raising Event

我有一个引发事件的函数

public void Delete(bool raiseEvent)
{
       if (raiseEvent)
       {
           RaiseEvent(new EdgeDeletedRoutedEventArgs(DeletedEvent, this));
       }
       ....some other stuff
}

我订阅该功能如下:

Class.Delete += SomethingDeleted;

void SomethingDeleted(object sender, EdgeDeletedRoutedEventArgs e)
{
    if (someCondition)
    {
        // here i want to cancel the delete function which raises the event
    }  
}

如何取消删除功能,以便在不中断我的应用程序的情况下不执行“其他内容”?

我试图在 SomethingDeleted 中抛出异常并在 Delete 中捕获异常,但它中断了我的应用程序并且我想保留应用程序 运行。

bool Cancel 属性 添加到 EdgeDeletedRoutedEventArgs(就像 CancelEventArgs 那样)

public void Delete(bool raiseEvent)
{
       if (raiseEvent)
       {
           var args = new EdgeDeletedRoutedEventArgs(DeletedEvent, this);
           RaiseEvent(args);
           if (args.Cancel) return;
       }
       ....some other stuff
}

void SomethingDeleted(object sender, EdgeDeletedRoutedEventArgs e)
{
    if (someCondition)
    {
         e.Cancel = true;
         return;
    }  
}

如果您真的不需要 RoutedEvent 的功能,请使用常规事件 (event EventHandler<EdgeDeletedEventArgs> Delete;) 并从 CancelEventArgs

继承 EdgeDeletedEventArgs