【问题标题】:CommandBinding: CustomCommand-Class is not foundCommandBinding:未找到 CustomCommand-Class
【发布时间】:2021-09-10 08:51:08
【问题描述】:

我即将学习 WPF 中的 CommandBindings,但不知何故我无法创建自定义命令。

显示以下错误:(XDG0008) 名称空间“clr-namespace:CommandBindingWPF”中不存在名称“CustomCommands”

<Window.CommandBindings>
    <CommandBinding Command="local:CustomCommands.Exit" CanExecute="ExitCommand_CanExecute" Executed="ExitCommand_Executed" />
</Window.CommandBindings>




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

//more code but not relevant for question

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

         private void ExitCommand_Executed(object sender, ExecutedRoutedEventArgs e)
         {
             Application.Current.Shutdown();
         }
     }

public static class CustomCommands
{
    public static readonly RoutedUICommand Exit = new RoutedUICommand
        (
            "Exit",
            "Exit",
            typeof(CustomCommands)
        );

    //Define more commands here, just like the one above
}

我什至尝试从https://wpf-tutorial.com/commands/implementing-custom-commands/ 复制粘贴它并粘贴CanExecuteExecuted 只是为了在MainWindow-class 中进行测试(当然我更改了xmlns:self),但它仍然可以不行。有人可以帮我吗?这让我发疯了。

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    Studio 中的 XAML 设计器不是从项目中获取有关类型的信息,而是从它们的程序集中获取信息。
    因此,如果您对项目进行了更改,那么在您对项目进行新的组装之前,设计师将不会看到它们。
    您无需为此关闭/打开 Studio。
    转到“项目”菜单,在那里选择“构建”。
    或者也可以在项目上下文菜单中的“解决方案资源管理器”中完成。
    此外,如果您对项目进行了更改,将在启动解决方案执行时自动执行新的构建。
    在极少数情况下(通常在出现一些错误、错误关闭 Studio 之后),在构建之前,您仍然需要对项目或解决方案执行“清理”。

    P.S. 由于 XAML 设计器的这种特殊性,为了不经常发现此类错误(在某些情况下它们可能会非常混乱,甚至导致编译错误),建议在其他项目中创建 XAML 中使用的所有类型。
    这使得更容易理解 XAML 设计器和编译器错误和警告的来源。

    【讨论】:

      【解决方案2】:

      我自己修好了! 我关闭了VS,重建了项目,错误没有再次弹出(WPF designer issues : XDG0008 The name "NumericTextBoxConvertor" does not exist in the namespace "clr-namespace:PulserTester.Convertors")。 我讨厌VS。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-11
        • 2017-03-13
        • 1970-01-01
        相关资源
        最近更新 更多