【发布时间】:2016-06-28 15:59:04
【问题描述】:
这就是我在我的 wpf 应用程序中实现快捷方式的方式:
public static class Shortcuts
{
static Shortcuts()
{
StartScanningCommand = new RoutedCommand();
StartScanningCommand.InputGestures.Add(new KeyGesture(Key.S, ModifierKeys.Control));
}
public readonly static RoutedCommand StartScanningCommand;
}
在我的 xaml 视图中,我有这个:
<Window.CommandBindings>
<CommandBinding Command="{x:Static local:Shortcuts.StartScanningCommand}" x:Name="StartScanningCommand" Executed="StartScanningCommand_Executed" CanExecute="StartScanningCommand_CanExecute"/>
</Window.CommandBindings>
在 xaml 的类中:
private void StartScanningCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
Scanner.Start();
}
private void StartScanningCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = AppCurrent.GetPermissionManager().CanScan();
if (!e.CanExecute)
{
AppCurrent.Broadcasts.ApplicationStatusBroadcast.NotifySubscribers(this, new ApplicationStatusEventArgs("You dont have permission to scan", StatusType.Error));
}
}
但由于某种原因StartScanningCommand_CanExecute 执行了两次。如果我在方法中添加MessageBox.Show,对话框会显示两次。
为什么会这样?
【问题讨论】: