WPF - 名称在命名空间中不存在

时间:2014-11-12 15:04:37

标签: c# wpf namespaces undefined

尝试在WPF中实现一些自定义绑定时,我遇到了命名空间问题。我收到错误'名称' CustomCommands'命名空间中不存在' clr-namespace:GraphicsBook; assembly = Testbed2D"。

在我的XAML中,我有:

<Window x:Class="GraphicsBook.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:k="clr-namespace:GraphicsBook;assembly=Testbed2D"
Title="Window1"

 <Window.CommandBindings>
    <CommandBinding Command="k:CustomCommands.AddCircle" CanExecute="AddCircleCommand_CanExecute" Executed="AddCircleCommand_Executed"></CommandBinding>
</Window.CommandBindings>

<Menu>
    <MenuItem Header="Add">
        <MenuItem Command="k:CustomCommands.AddCircle" />
    </MenuItem>
</Menu>

我的CustomsCommand.cs文件位于项目文件夹中。在这个文件中是:

namespace GraphicsBook
{
    public partial class CustomCommandSample : Window
    {
        public CustomCommandSample()
        {
            ...
        }

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

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

错误来自&#39; MenuItem Command =&#34; k:CustomCommands.AddCircle&#34;&#39;。

任何帮助都将非常感谢!!

1 个答案:

答案 0 :(得分:1)

您的XML / CLR命名空间映射错误:您有k别名GraphicsBook,但CustomCommands中声明了GraphicsBook.Assignment

您也可以尝试使用{x:Static k:CustomCommands.AddCircle}而不是k:CustomCommands.AddCircle