我有一个遵循MVVM模式的WPF应用程序。我需要实现键盘快捷键。这些快捷键必须控制WebBrowser控件的行为。我定义了第一个自定义命令并将其添加到视图的输入绑定中。还将有更多命令需要调用浏览器上的脚本:
MainWindow.xaml.cs:
...
CommandBinding cb = new CommandBinding(RemoteControlCommands.TestCommand, MyCommandExecuted, MyCommandCanExecute);
this.CommandBindings.Add(cb);
KeyGesture kg = new KeyGesture(Key.Q, ModifierKeys.Control);
InputBinding ib = new InputBinding(RemoteControlCommands.TestCommand, kg);
this.InputBindings.Add(ib);
}
private void MyCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
webBrowser.InvokeScript("foo", "Hello World!");
}
private void MyCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
我的问题是如何将这个问题融入MVVM模式? MVVM对我来说是一个新概念,但我了解如何将视图的命令绑定到视图模型,并在那里执行方法或更改属性。然而,在这种情况下,我需要在视图中的控件上执行一个方法。在这种情况下,最好的快捷方式处理位置在哪里?
KeyBinding.Command
和KeyBinding.CommandParameter
不是依赖属性。 - Rafael Costa