本地命名空间中不存在XAML CommandBinding?

时间:2016-09-26 01:15:03

标签: c# wpf xaml

我试图在C#中为我的项目添加自定义命令。为此,我在项目中添加了一个新类:

using System.Windows.Input;

namespace DataBindingHuiswerk
{
    public static class CustomCommands
    {
        public static readonly RoutedUICommand Add = new RoutedUICommand(
            "Add",
            "Add",
            typeof(CustomCommands),
            new InputGestureCollection()
            {
                new KeyGesture(Key.F1, ModifierKeys.Control)
            }
        );

        public static readonly RoutedUICommand Change = new RoutedUICommand(
            "Change",
            "Change",
            typeof(CustomCommands),
            new InputGestureCollection()
            {
                new KeyGesture(Key.F2, ModifierKeys.Control)
            }
        );

        public static readonly RoutedUICommand Delete = new RoutedUICommand(
            "Delete",
            "Delete",
            typeof(CustomCommands),
            new InputGestureCollection()
            {
                new KeyGesture(Key.F3, ModifierKeys.Control)
            }
        );
    }
}

接下来我试图在我的XAML代码中绑定它们:

<Window x:Class="DataBindingHuiswerk.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DataBindingHuiswerk"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <CommandBinding Command="local:CustomCommands.Add" Executed="AddCommand_Executed" CanExecute="AddCommand_CanExecute" />

然而,这让我回头:

  

名称&#34; CustomCommands&#34;命名空间中不存在&#34; clr-namespace:DataBindingHuiswerk&#34;

现在我在这里可能错了,但对CustomCommands DataBindingHuiswerk明确存在于static var cache = NSCache<NSString, UIImage>() 命名空间内?我错过了什么吗?

2 个答案:

答案 0 :(得分:2)

事实证明,我忘了将<CommandBinding />包裹在<Window.CommandBindings />内。该问题的解决方案是:

<Window.CommandBindings>
    <CommandBinding Command="local:CustomCommands.Add" Executed="AddCommand_Executed" CanExecute="AddCommand_CanExecute" />
</Window.CommandBindings>

通常情况下我会删除这个问题,因为它是一个简单的印刷错误,但是(正如Ed Plunkett在评论中正确指出的那样)错误信息对于什么是真正的错误是非常误导的上。因此,我会保持原样。

答案 1 :(得分:2)

代码中的一些问题。

Xaml应该是这样的。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.CommandBindings>
        <CommandBinding Command="local:CustomCommands.Add" Executed="AddCommand_Executed" CanExecute="CommandBinding_OnCanExecute" />
    </Window.CommandBindings>
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <Button Command="local:CustomCommands.Add">Add</Button>
    </StackPanel>
</Window>

背后的代码应该是这样的。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void AddCommand_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        Debug.WriteLine("sdfgsdgdsgdf");
    }

    private void CommandBinding_OnCanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
    }
}