Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 值不能为null CommandBinding自定义命令_C#_Wpf - Fatal编程技术网

C# 值不能为null CommandBinding自定义命令

C# 值不能为null CommandBinding自定义命令,c#,wpf,C#,Wpf,我几天前刚开始使用WPF,遇到了一个我不理解的问题 我得到了以下错误: 值不能为null。参数名称:值 错误发生在以下位置: <Window.CommandBindings> <CommandBinding Command="self:CustomCommands.Exit" Executed="ExitCommand_Executed" CanExecute="ExitCommand_CanExecute"/> </Window.CommandBi

我几天前刚开始使用WPF,遇到了一个我不理解的问题

我得到了以下错误:

值不能为null。参数名称:值

错误发生在以下位置:

<Window.CommandBindings>
        <CommandBinding Command="self:CustomCommands.Exit" Executed="ExitCommand_Executed" CanExecute="ExitCommand_CanExecute"/>
</Window.CommandBindings>
那么,为什么在使用自定义命令时会发生此错误,而在使用例如
command=“ApplicationCommands.New”
时不会发生此错误?如何修复此错误


代码是的一部分。

可能需要将CustomCommand设置为非静态

并将主窗口的DataContext设置为CustomCommand

 public class CustomCommands
 {
        public static readonly RoutedUICommand Exit = new RoutedUICommand
                (
                        "Beenden",
                        "Exit",
                        typeof(CustomCommands),
                        new InputGestureCollection()
                        {
                            new KeyGesture(Key.F4, ModifierKeys.Alt)
                        }
                );
 }

public partial class MainWindow : Window
{
    public  CustomCommands CM;
    public MainWindow()
    {
        CM = new  CustomCommands();
        this.DataContext = CM;
        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();
    }

}
单击设计器窗口下的“启用项目代码”按钮


我遇到了这个问题,然后我只是清理然后构建解决方案,然后它就消失了。作为将来的参考,我发现使用VisualStudio的WPF的许多问题都是通过先清理后构建解决的,不知道为什么WPF+VS会更频繁地出现这些问题。

您使用的是什么版本的VS?根据教程和提供的代码片段,我无法重现您的错误。Vs 2015 Enterprise V 14.0。我应该补充一点,项目已经编译并运行,但错误仍然存在。我现在已经创建了一个新项目,并以1:1的比例复制,错误已经消失了。。。我想是智能感应虫吧!?可能是,我记得在旧版本的VS中,我不时会遇到这个错误,通常会通过重新启动VS来消除。在VS2017中,在设计视图和XAML视图之间切换启用项目代码按钮来修复它。
 public class CustomCommands
 {
        public static readonly RoutedUICommand Exit = new RoutedUICommand
                (
                        "Beenden",
                        "Exit",
                        typeof(CustomCommands),
                        new InputGestureCollection()
                        {
                            new KeyGesture(Key.F4, ModifierKeys.Alt)
                        }
                );
 }

public partial class MainWindow : Window
{
    public  CustomCommands CM;
    public MainWindow()
    {
        CM = new  CustomCommands();
        this.DataContext = CM;
        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();
    }

}