selectionChanged 事件因未知原因触发异常 [英] selectionChanged event firing exceptions for unknown reasons
问题描述
你好,我遇到了一个问题,每当我尝试在 TabControl_SelectionChanged 事件中编写任何简单的东西时,我都会收到此消息
hello i got a problem, whenever i try to write any simple thing inside the TabControl_SelectionChanged event i am getting this message
WindowsBase.dll 中发生类型为System.InvalidOperationException"的未处理异常
An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll
附加信息:调度程序处理已暂停,但仍在处理消息.
Additional information: Dispatcher processing has been suspended, but messages are still being processed.
例如:这是我的 xaml:
for example: this is my xaml:
<Window x:Class="try1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="auto" Width="auto" xmlns:my="clr-namespace:try1" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="171" d:DesignWidth="271" SizeToContent="WidthAndHeight">
<TabControl SelectionChanged="TabControl_SelectionChanged">
<CheckBox Name="cbx"></CheckBox>
<TabItem Header="tabItem1" Name="tabItem1">
<Grid />
</TabItem>
<TabItem Header="tabItem2" Name="tabItem2">
<Grid />
</TabItem>
</TabControl>
这是我背后的代码:
private void something_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("hello");
}
预先感谢您的帮助
推荐答案
那是因为在加载选项卡控件(和窗口)之前触发了 SelectionChanged 事件.如果你这样做,它就可以工作(对不起 VB.NET 示例,但你明白了):
That's because you SelectionChanged event gets fired before the tabcontrol (and window) is loaded. If you do it like this, it works (sorry for the VB.NET sample, but you get the point):
Private Sub TabControl_SelectionChanged(ByVal sender As Object, ByVal e As RoutedEventArgs)
If Me.IsLoaded Then
MsgBox("hello")
End If
End Sub
是的,原因在评论中提到的新闻组帖子中有解释.
And yes, the reason is explained in the newsgroup post mentioned in the comment.
这篇关于selectionChanged 事件因未知原因触发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!