gpt4 book ai didi

c# - 为什么在我将 RoutedEventArgs 事件标记为已处理时引发异常?

转载 作者:行者123 更新时间:2023-11-30 17:08:57 26 4
gpt4 key购买 nike

我正在尝试将事件标记为已处理,但在我尝试这样做时抛出了异常。

我在 LostFocus 上调用了以下函数TextBox 的事件:

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

e.Handled = true;
}

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

An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationCore.dll

Additional information: Every RoutedEventArgs must have a non-null RoutedEvent associated with it.

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

为什么会引发异常,有哪些可能的解决方法?

最佳答案

想通了。 TextBox_LostFocus 从应用程序的其他地方调用,事件参数为 null

因此,对于 TextBox_LostFocus 的某些函数调用,e.Handled 不起作用(因为 e 为 null)但在一些函数中起作用其他场景(当它实际被框架调用时)。

关于c# - 为什么在我将 RoutedEventArgs 事件标记为已处理时引发异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13384899/

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