当我在.NET Framework 4.7.2中创建一个新的WPF应用程序并将菜单添加到MainWindow时,每当单击菜单然后单击菜单以关闭它时,我会收到
InvalidCastException
。当此异常从PresentationFramework.dll
抛出时,我可以忽略它,并且一切都很好。我只是想知道这是否是.NET Framework问题还是我做错了什么?整个解决方案可以在此处找到https://github.com/glrad/InvalidCastException。
App.xaml.cs
protected override void OnStartup(StartupEventArgs e)
{
var view = new MainWindow();
MainWindow = view;
view.Show();
}
MainWindow.xaml
<Window x:Class="MyApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="GASGC3" Height="800" Width="1000">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Menu Grid.Row="0">
<MenuItem Header="File">
<MenuItem Header="Options" InputGestureText="Ctrl+O"/>
<Separator/>
<MenuItem Header="Exit" InputGestureText="Alt+F4" />
</MenuItem>
<MenuItem Header="Help">
<MenuItem Header="About" />
</MenuItem>
</Menu>
</Grid>
</Window>
抛出异常:
System.InvalidCastException:“无法将类型为'System.Collections.Hashtable'的对象强制转换为类型'System.String'。”
MainWindow
会运行两次(因为你重写了OnStartup
方法)。 - Pavel Anikhouski