【问题标题】:XAML CommandBinding does not exist within local namespace?本地命名空间中不存在 XAML CommandBinding?
【发布时间】:2016-09-26 01:15:03
【问题描述】:

我正在尝试将自定义命令添加到我的 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" />

但是,这会返回我:

命名空间“clr-namespace:DataBindingHuiswerk”中不存在名称“CustomCommands”

现在我可能在这里错了,但对我来说 CustomCommands 显然存在于 DataBindingHuiswerk 命名空间中?我错过了什么吗?

【问题讨论】:

  • “返回我”是否意味着它不会构建?
  • @EdPlunkett 不,这意味着我的项目显示“无效标记”,当将鼠标悬停在错误下划线部分时:Command="local:CustomCommands.Add" 它会返回该消息。
  • 错误信息具有如此离奇的误导性这一事实表明该问题值得保留。
  • @KosalaW 好的,我不会。
  • @EdPlunkett 如果你真的做队友,整个世界都注定要失败,所以交给我们其他人吧 xD

标签: c# wpf xaml


【解决方案1】:

原来我只是忘记将&lt;CommandBinding /&gt; 包裹在&lt;Window.CommandBindings /&gt; 中。问题的解决方案是:

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

通常我会删除这个问题,因为它是一个简单的印刷错误,但是(正如 Ed Plunkett 在 cmets 中正确指出的那样)错误消息对于真正发生的事情非常具有误导性。因此,我将保持原样。

【讨论】:

    【解决方案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;
        }
    }
    

    【讨论】:

    • 是的,我知道这些部分。正如我在自己的回答中指出的那样,问题确实是 xaml 中缺少 &lt;Window.CommandBindings&gt; 。我会将您的问题标记为已回答,这样问题就不会悬而未决。非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    • 2013-01-17
    • 2015-04-28
    • 2011-10-19
    • 1970-01-01
    相关资源
    最近更新 更多