【问题标题】:WPF - Name doesn't exist in namespaceWPF - 名称在命名空间中不存在
【发布时间】:2014-11-12 15:04:37
【问题描述】:

我在尝试在 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)
                            }
            );
    }
}

错误来自'MenuItem Command="k:CustomCommands.AddCircle"'行。

任何帮助将不胜感激!

【问题讨论】:

    标签: c# wpf namespaces undefined


    【解决方案1】:

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

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

    【讨论】:

    • CustomCommands 是在Testbed2D 程序集中声明的?
    • 是的,它是 Testbed2D 中的 .cs 文件
    • 你试过x:Static吗?
    • 嗯,不幸的是仍然给我同样的错误Command="{x:Static k:CustomCommands.AddCircle}"
    • 出了点问题。窗口是在同一个组件中,还是在不同的组件中?如果是不同的程序集,该命令的程序集是否被包含该窗口的项目引用?您确定程序集名称与项目名称(Testbed2D)匹配吗?如果您重命名了一个,则需要重命名另一个。
    猜你喜欢
    • 2016-03-08
    • 2014-10-19
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多