<Window x:Class="CostelloM_Data_Persistence_v1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ContactMyPeeps(IllegalVersion)" Height="350" Width="525">
<Window.Resources>
<RoutedCommand x:Key="Saveas"/>
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="Saveas" Executed="Save_As"/>
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Key="S"
Modifiers="Control + Shift"
Command="Saveas"/>
</Window.InputBindings>
<Grid>
<StackPanel Orientation="Vertical">
<Menu>
<MenuItem Header="File">
<MenuItem Command="Save" Header="Save"/>
<MenuItem Command="Saveas" Header="Save as" InputGestureText="Ctrl+Shift+S"/>
<MenuItem Command="Open" Header="Open"/>
</MenuItem>
</Menu>
<ItemsControl>
<ComboBox>
</ComboBox>
</ItemsControl>
</StackPanel>
</Grid>
</Window>
我的问题如下,上面的代码无法编译。我尝试过保存、清理和重建我的visual studio项目,但仍然没有成功。它说命令转换器不能从system.string转换。很明显,我要么误解了RoutedCommand,不能以这种方式使用自定义命令。是否有一种方法可以强制RoutedCommand创建一个新命令,或者有一种不同的方法来使用自定义命令?
在资源中定义命令,因此需要告诉绑定系统它在资源中。您需要将XAML中的几个位置更改为
Command="{StaticResource Saveas}"
但是,在ApplicationCommands
类中为您预先定义了几个标准命令,如Open、Save和savea。绑定系统将自动尝试绑定这些,但套管很重要。:
Command="SaveAs"
将绑定到ApplicationCommands
中定义的适当命令。那么在资源中定义的命令就没有必要了。
您有几行错字,这很可能会给您一个"CommandConverter无法从System转换。字符串"错误,因为它不识别命令。
改变:
<CommandBinding Command="Saveas" Executed="Save_As"/>
To this: (大写"A")
<CommandBinding Command="SaveAs" Executed="Save_As"/>