首先,这是我的代码:
<Window ...>
<Window.Resources>
</Window.Resources>
<Window.InputBindings>
<KeyBinding Key="Space" Command="{Binding KeyboardCommand}" CommandParameter="Space"/>
<KeyBinding Key="OemPeriod" Command="{Binding KeyboardCommand}" CommandParameter="Blank"/>
<KeyBinding Key="D0" Command="{Binding KeyboardCommand}" CommandParameter="Rest"/>
<KeyBinding Key="D1" Command="{Binding KeyboardCommand}" CommandParameter="N1"/>
<KeyBinding Key="D2" Command="{Binding KeyboardCommand}" CommandParameter="N2"/>
<KeyBinding Key="D3" Command="{Binding KeyboardCommand}" CommandParameter="N3"/>
<KeyBinding Key="D4" Command="{Binding KeyboardCommand}" CommandParameter="N4"/>
<KeyBinding Key="D5" Command="{Binding KeyboardCommand}" CommandParameter="N5"/>
<KeyBinding Key="D6" Command="{Binding KeyboardCommand}" CommandParameter="N6"/>
<KeyBinding Key="D7" Command="{Binding KeyboardCommand}" CommandParameter="N7"/>
<KeyBinding Modifiers="Control" Key="Space" Command="{Binding KeyboardCommand}" CommandParameter="Space"/>
<KeyBinding Modifiers="Control" Key="OemPeriod" Command="{Binding KeyboardCommand}" CommandParameter="Blank"/>
<KeyBinding Modifiers="Control" Key="D0" Command="{Binding KeyboardCommand}" CommandParameter="Rest"/>
<KeyBinding Modifiers="Control" Key="D1" Command="{Binding KeyboardCommand}" CommandParameter="N1"/>
<KeyBinding Modifiers="Control" Key="D2" Command="{Binding KeyboardCommand}" CommandParameter="N2"/>
<KeyBinding Modifiers="Control" Key="D3" Command="{Binding KeyboardCommand}" CommandParameter="N3"/>
<KeyBinding Modifiers="Control" Key="D4" Command="{Binding KeyboardCommand}" CommandParameter="N4"/>
<KeyBinding Modifiers="Control" Key="D5" Command="{Binding KeyboardCommand}" CommandParameter="N5"/>
<KeyBinding Modifiers="Control" Key="D6" Command="{Binding KeyboardCommand}" CommandParameter="N6"/>
<KeyBinding Modifiers="Control" Key="D7" Command="{Binding KeyboardCommand}" CommandParameter="N7"/>
...
</Window.InputBindings>
<Grid>
...
</Grid>
这些代码没有错误,所以它工作得很好。但是,如果我的应用程序InputBindings
有很多InputBindings
.
那么,是否可以(以任何方式)简化/缩短它们?那是因为我的应用会需要很多InputBindings
/的修饰符组合,感觉一个一个输入会显得“不整齐”。米KeyBinding
或者也许这是唯一的方法(使用 MVVM)?
请澄清任何需要的东西:D
以防万一,这些是 Command 和 ViewModel 类中的相关方法:
public void Execute(object parameter)
{
Notes note;
if (Enum.TryParse(parameter.ToString(), out note))
{
_vm.AddOrUpdateNote(note, Keyboard.Modifiers);
}
else
{
...
}
}
我的 ViewModel 的一部分:
public void AddOrUpdateNote(Notes note, ModifierKeys mKeys)
{
if (mKeys == ModifierKeys.Control)
{
...
}
else if (mKeys == ModifierKeys.Shift)
{
...
}
else
{
...
}
}
因此,根据按下哪个修饰键,会有细微的行为差异。(拆分成不同的方法对我来说感觉很糟糕)
更新:我已经阅读了一些解释InputGestures
。在http://msdn.microsoft.com/en-us/library/ms752308%28v=vs.110%29.aspx据说this.InputBindings.Add(Blabla)
(我猜来自 xaml.cs),它可以在 ViewModel 中完成吗?或者它是否严格需要通过 XAML 完成,所以,如果我的应用程序中有很多组合键,例如上面的示例,它仍然需要以“长”方式完成吗?
如果可能,请提供一些代码示例,以便我更好地理解它。谢谢
(不太清楚怎么问,所以请随时澄清)