2016-09-26 47 views
1

我想在C#中将自定义命令添加到我的项目中。要做到这一点,我已经添加了一个新的类,我的项目:XAML CommandBinding在本地命名空间中不存在?

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" /> 

然而,这将返回我:

名称 “CustomCommands” 没有命名空间中存在 “CLR的命名空间:DataBindingHuiswerk”

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

+0

“回报我”是否意味着它不会建立? –

+0

@EdPlunkett不,这意味着我的项目显示“Invalid Markup”,并且将鼠标悬停在错误下划线部分上:'Command =“local:CustomCommands.Add”'它会返回该消息。 – icecub

+0

啊。它会建立吗? –

回答

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; 
    } 
} 
+0

我知道这些部分。问题确实是''从我自己的答案中指出的xaml中丢失。我会将你的标记标记为已回答,所以问题不会得到解答。非常感谢 :) – icecub

2

事实证明,我只是忘了把<CommandBinding />换成<Window.CommandBindings />。这个问题的解决办法是:

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

通常我会已经删除的问题,因为它是一个简单的打字错误,但(爱德宾吉正确的评论中指出的)错误信息是什么是非常误导真的在继续。为此我会保持完好。