Home > OS >  CommandBinding: CustomCommand-Class is not found
CommandBinding: CustomCommand-Class is not found

Time:09-16

I am about to learn CommandBindings in WPF, but somehow I cannot create custom commands.

Following error is shown: (XDG0008) The name "CustomCommands" does not exist in the namespace "clr-namespace:CommandBindingWPF"

<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
}

I even tried to copy-paste it from https://wpf-tutorial.com/commands/implementing-custom-commands/ and paste the CanExecute and Executed just for testing in MainWindow-class (and of course i changed the xmlns:self), but it still does not work. Can someone help me out? It is making me crazy.

CodePudding user response:

I fixed it by myself! I closed VS, rebuilt the project and the error did not pop up again (WPF designer issues : XDG0008 The name "NumericTextBoxConvertor" does not exist in the namespace "clr-namespace:PulserTester.Convertors"). I hate VS.

CodePudding user response:

The XAML designer in Studio takes information about types not from projects, but from their assemblies.
Therefore, if you made a change to the project, then the Designer will not see them until you make a new assembly of the project.
You do not need to close / open the Studio for this.
Go to the "Project" menu, select "Build" there.
Or the same can be done in the "Solution Explorer" in the context menu of the Project.
Also, if you made changes to the project, a new build will be performed automatically when the Solution is launched for execution.
In very rare cases (usually after some bugs, incorrect closing of the Studio), before building, you still need to perform a "Cleanup" of the Project or Solution.

P.S. Due to this peculiarity of the XAML Designer, in order not to constantly stumble upon such errors (and in some cases they can be very confusing and even lead to compilation errors), it is recommended to create all types used in XAML in other projects. This makes it much easier to understand the source of the XAML Designer and Compiler errors and warnings.

  • Related