当我将RoutedEventArgs事件标记为Handled时,为什么会引发异常?

时间:2012-11-14 18:23:44

标签: c# wpf wpf-controls

我正在尝试将事件标记为已处理,但在尝试执行此操作时会抛出异常。

我在TextBox的{​​{3}}事件中调用了以下函数:

private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
   // ...
   // do something
   // ...

   e.Handled = true;
}

但是,只要执行e.Handled = true,就会引发以下异常:

  

类型'System.InvalidOperationException'的未处理异常   发生在PresentationCore.dll

     

其他信息:每个RoutedEventArgs必须具有非null值   与之关联的RoutedEvent。

我尝试在函数中的其他代码之前移动e.Handled行,但这没有帮助。

为什么会引发异常以及可能的解决方法?

1 个答案:

答案 0 :(得分:1)

想出来。正在从应用程序的其他位置调用TextBox_LostFocus,其中事件参数为null

因此,对于TextBox_LostFocus的某些函数调用,e.Handled没有工作(因为e为空),但在其他一些情况下(当它实际调用时)框架)。

相关问题