我只是写了一个扩展类,扩展了3个方法,这三个方法可以将命令和热键一同绑定到View而不需要等View加载完。
namespace WpfApplication1
{
public static class ScreenCommandExtension
{
public static void RegistHotKey(this Screen screen, ICommand command, Key key, ModifierKeys modifiers)
{
Type viewType = ViewLocator.LocateTypeForModelType(screen.GetType(), null, null);
CommandManager.RegisterClassInputBinding(viewType, new InputBinding(command, new KeyGesture(key, modifiers)));
}
public static void RegistUICommand(this Screen screen, RoutedUICommand command, ExecutedRoutedEventHandler excute, CanExecuteRoutedEventHandler canExcute)
{
Type viewType = ViewLocator.LocateTypeForModelType(screen.GetType(), null, null).GetType();
CommandManager.RegisterClassCommandBinding(viewType, new CommandBinding(command, excute, canExcute));
}