我几天前才开始使用 WPF,遇到了一个我不明白的问题。
我收到以下错误:
Value cannot be null. Parametername: value
错误发生在这里:
<Window.CommandBindings>
<CommandBinding Command="self:CustomCommands.Exit" Executed="ExitCommand_Executed" CanExecute="ExitCommand_CanExecute"/>
</Window.CommandBindings>
我当然在 xaml 中设置了命名空间 xmlns:self="clr-namespace:PrintMonitor"
。
代码隐藏:
namespace PrintMonitor
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ExitCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if(e != null)
e.CanExecute = true;
}
private void ExitCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
Application.Current.Shutdown();
}
}
public static class CustomCommands
{
public static readonly RoutedUICommand Exit = new RoutedUICommand
(
"Beenden",
"Exit",
typeof(CustomCommands),
new InputGestureCollection()
{
new KeyGesture(Key.F4, ModifierKeys.Alt)
}
);
}
}
那么为什么如果我使用自定义命令会出现此错误,而如果我使用例如Command="ApplicationCommands.New"
以及如何修复此错误?
代码是 this tutorial 的一部分.
最佳答案
关于c# - 值不能为空 CommandBinding 自定义命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35035195/