1

I am having a namespace issue while trying to implement some custom bindings in WPF. I am getting the error 'The name 'CustomCommands' does not exist in the namespace 'clr-namespace:GraphicsBook;assembly=Testbed2D".

In my XAML I have:

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

And my CustomsCommand.cs file is within the project folder. Within this file is:

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

The error comes from the line 'MenuItem Command="k:CustomCommands.AddCircle"'.

Any help would be much appreciated!!

petehallw
  • 1,014
  • 6
  • 21
  • 49

1 Answers1

1

Your XML/CLR namespace mapping is wrong: you have k aliasing GraphicsBook, but CustomCommands is declared in GraphicsBook.Assignment.

You can also try using {x:Static k:CustomCommands.AddCircle} instead of simply k:CustomCommands.AddCircle.

Mike Strobel
  • 25,075
  • 57
  • 69
  • And `CustomCommands` is declared in the `Testbed2D` assembly? – Mike Strobel Nov 12 '14 at 15:17
  • Yeah it is a .cs file within Testbed2D – petehallw Nov 12 '14 at 15:25
  • Did you try using `x:Static`? – Mike Strobel Nov 12 '14 at 15:32
  • Hm that still gives me the same error unfortunately `Command="{x:Static k:CustomCommands.AddCircle}"` – petehallw Nov 12 '14 at 15:40
  • Something is wrong. Is the window in the same assembly, or a different assembly? If a different assembly, is the command's assembly referenced by the project containing the window? Are you sure the assembly name matches the project name (Testbed2D)? If you renamed one, you need to rename the other. – Mike Strobel Nov 12 '14 at 15:43
  • My window is in one project and Testbed2D is another project, containing the CustomCommands.cs file. I tried putting the CustomCommands.cs file in the project with the window but that didn't seem to help. How might I go about referencing the command's assembly? Many thanks for your help – petehallw Nov 12 '14 at 15:55
  • You need to add a project reference to your window's project that points to the project with the command. Right click your window's Project -> References node in Solution Explorer and click Add Reference. – Mike Strobel Nov 12 '14 at 15:58