我正在使用 Prism 并且有CompositeCommand
一个ApplicationCommands.cs
班级:
public CompositeCommand ShowSourceFormattingCommand { get; } = new CompositeCommand(true);
我有一个DelegateCommand<bool?>
注册到这个CompositeCommand
:
public DelegateCommand<bool?> ShowSourceFormattingCommand { get; }
...
Services.ApplicationCommands.ShowSourceFormattingCommand.
RegisterCommand(ShowSourceFormattingCommand);
然后它与一个命令处理程序相关联:
ShowSourceFormattingCommand =
new DelegateCommand<bool?>(changeDisplayCommandsHandler.OnShowSourceFormattingSelect).
ObservesCanExecute(() => IsActive);
...
public void OnShowSourceFormattingSelect(bool? selected)
{
Services.EventService.GetEvent<ShowSourceFormattingEvent>().Publish(selected ?? false);
}
它是绑定到ToggleButton
UI 中的 a 的数据,并且运行良好。但是,当我尝试将键盘快捷键与其关联时,它不起作用(使用指定的键)。
<KeyBinding Modifiers="Ctrl+Shift" Key="S"
Command="{Binding ShowSourceFormattingCommand}" />
这是因为bool参数没有值,所以为null。如果在 UI 中打开了该选项,则键盘快捷键会将其切换为关闭,但永远不会重新打开。请注意,ComandParameter
该类KeyBinding
的 没有传递给关联的命令,但如果是这样就无济于事,因为我需要它在真假之间交替。
<KeyBinding Modifiers="Ctrl+Shift" Key="S" Command="{Binding ShowSourceFormattingCommand}"
CommandParameter="True" />
因此,我尝试实现该CommandReference
对象,如如何将按键与复合 WPF 中的 DelegateCommand 关联?,但它给出了相同的结果,可为空的 bool 参数始终为空。
然后我尝试为 实现另一个命令KeyBinding
,这将切换值:
public CompositeCommand ShowSourceFormattingKeyboardCommand { get; } =
new CompositeCommand(true);
...
public DelegateCommand ShowSourceFormattingKeyboardCommand { get; }
...
Services.ApplicationCommands.ShowSourceFormattingKeyboardCommand.
RegisterCommand(ShowSourceFormattingKeyboardCommand);
...
ShowSourceFormattingKeyboardCommand =
new DelegateCommand(changeDisplayCommandsHandler.OnToggleShowSourceFormattingCommand).
ObservesCanExecute(() => IsActive);
...
private bool _isSourceFormattingShown = false;
public void OnToggleShowSourceFormattingCommand()
{
_isSourceFormattingShown = !_isSourceFormattingShown;
OnShowSourceFormattingSelect(_isSourceFormattingShown);
}
这可以正常工作并正确打开和关闭该功能,但在使用键盘快捷键时按钮状态没有任何指示。这对于所有这些方法都是一样的。我的问题是这些可为空的 bool 命令应该如何连接到 aToggleButton
以正确更新按钮的视觉状态,例如。打开和关闭按钮?