我试图通过创建WindowsFormsHost并在其中添加一个MenuStrip来在C#中使用WPF创建一个MenuBar。请注意,我使用WindowsFormsHost控制器的属性页添加菜单和菜单项。
当我完成时,项目构建并成功运行,但它在生成的" xaml"中显示了一些错误。文件和设计师没有加载设计。
这里是生成的XAML代码
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" x:Class="FrontEnd.AppMainWindow"
Title="AppMainWindow" Height="351" Width="826">
<Grid>
<WindowsFormsHost x:Name="menuStripHost" HorizontalAlignment="Left" Height="23" VerticalAlignment="Top" Width="818">
<Forms:MenuStrip Visible="True" Name="appMainMenu">
<Forms:MenuStrip.Items>
<Forms:ToolStripMenuItem Text="File">
<Forms:ToolStripMenuItem.DropDownItems>
<Forms:ToolStripMenuItem ShortcutKeys="Ctrl+N" Text="New Project"/>
<Forms:ToolStripMenuItem ShortcutKeys="Ctrl+O" Text="Open Project"/>
<Forms:ToolStripMenuItem Text="Exit"/>
</Forms:ToolStripMenuItem.DropDownItems>
</Forms:ToolStripMenuItem>
<Forms:ToolStripMenuItem Text="View">
<Forms:ToolStripMenuItem.DropDownItems>
<Forms:ToolStripMenuItem Text="Configuration Window"/>
</Forms:ToolStripMenuItem.DropDownItems>
</Forms:ToolStripMenuItem>
<Forms:ToolStripMenuItem Text="Help">
<Forms:ToolStripMenuItem.DropDownItems>
<Forms:ToolStripMenuItem Text="User Manual"/>
<Forms:ToolStripMenuItem Text="Help"/>
</Forms:ToolStripMenuItem.DropDownItems>
</Forms:ToolStripMenuItem>
</Forms:MenuStrip.Items>
</Forms:MenuStrip>
</WindowsFormsHost>
</Grid></Window>
以及错误
错误2无法将指定的值分配给集合。期望以下类型:&#34;字符串&#34;。
对于具有TAG&#34; Forms:ToolStripMenuItem&#34;
的所有行都会出现相同的错误更新 试图删除ShortcutKeys属性,问题仍然存在(与上面相同的错误)。 这是修改后的代码......
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" x:Class="FrontEnd.AppMainWindow"
Title="AppMainWindow" Height="351" Width="826">
<Grid>
<WindowsFormsHost x:Name="menuStripHost" HorizontalAlignment="Left" Height="23" VerticalAlignment="Top" Width="818">
<Forms:MenuStrip Visible="True" Name="appMainMenu">
<Forms:MenuStrip.Items>
<Forms:ToolStripMenuItem Text="File">
<Forms:ToolStripMenuItem.DropDownItems>
<Forms:ToolStripMenuItem Text="New Project"/>
<Forms:ToolStripMenuItem Text="Open Project"/>
<Forms:ToolStripMenuItem Text="Exit"/>
</Forms:ToolStripMenuItem.DropDownItems>
</Forms:ToolStripMenuItem>
<Forms:ToolStripMenuItem Text="View">
<Forms:ToolStripMenuItem.DropDownItems>
<Forms:ToolStripMenuItem Text="Configuration Window"/>
</Forms:ToolStripMenuItem.DropDownItems>
</Forms:ToolStripMenuItem>
<Forms:ToolStripMenuItem Text="Help">
<Forms:ToolStripMenuItem.DropDownItems>
<Forms:ToolStripMenuItem Text="User Manual"/>
<Forms:ToolStripMenuItem Text="Help"/>
</Forms:ToolStripMenuItem.DropDownItems>
</Forms:ToolStripMenuItem>
</Forms:MenuStrip.Items>
</Forms:MenuStrip>
</WindowsFormsHost>
</Grid></Window>
答案 0 :(得分:0)
我认为在你的ToolStripMenuItems中,你要为ShortcutKeys属性分配一个字符串:ShortcutKeys =“Ctrl + N”......根据文档,这应该是'Keys'类型; https://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripmenuitem.shortcutkeys%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396