0

I want to do a command binding in my WPF project:

1) I created the following module

Namespace Test
    Module CustomCommands
        Public ReadOnly Export As RoutedUICommand = New RoutedUICommand(
            "Export",
            "Export",
            GetType(CustomCommands),
            New InputGestureCollection(New KeyGesture(Key.F7, ModifierKeys.Alt))
        )
    End Module
End Namespace

2) In my main window I created a CommandBinding:

<Window.CommandBindings>
        <CommandBinding Command="local:CustomCommands.Export" CanExecute="ExportCommand_CanExecute" Executed="ExportCommand_Executed"></CommandBinding>
    </Window.CommandBindings>

3) In a button in my main window I added the binding:

Button Command="CustomCommands.Export">Exit</Button>

My problem is at point 2 Visual Studio is telling me: The name "CustomCommands" does not exist in the namespace "clr-namespace:Test" even though my main window is part of this namespace:

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Test"
<...></...>
</Window>

What am I doing wrong?

I followed several advices here to switch from Debug to Release or from x86 to x64 and recompile but none of them where solving my problem.

UPDATE Thanks to mm8's answer I removed Namespace Test from the module CustomCommands and did a rebuild. Now it works without error.

bluefox
  • 175
  • 3
  • 16
  • Your code shows the namespace is TestFullscreen, rather than just Test. Is that the problem? – Sean Beanland May 08 '18 at 13:02
  • Sorry this was my mistake. Both are "Test" – bluefox May 08 '18 at 13:11
  • Then it should be `xmlns:local="clr-namespace:Test.Test"`. See my answer. – mm8 May 08 '18 at 13:11
  • Why `Test.Test`? My Main Window is in namespace `Test`. And my custom command class is also part of `Test` as you can see in my code snippets. Is it not enough to write `xmlns:local="clr-namespace:Test" ` ? – bluefox May 08 '18 at 13:18
  • When I align both namespace names, I get a new error : **CustomCommands is not supported in a Windows Presentation Foundation (WPF) project"** – bluefox May 08 '18 at 13:19
  • If you remove "Namespace Test" from your Module it's enough to write "Test". Otherwise it's not as stated in my answer. The first "Test" refers to the default namespace of your application. This is typically the same as the name of the project. – mm8 May 08 '18 at 13:40

1 Answers1

1

Add Test to the default namespace in the namespace declaration:

xmlns:local="clr-namespace:Test.Test"

...and pass a List(Of KeyGesture) to the InputGestureCollection that you are creating in your module:

Namespace Test 
    Public Module CustomCommands
        Public ReadOnly Export As RoutedUICommand = New RoutedUICommand(
            "Export",
            "Export",
            GetType(CustomCommands),
            New InputGestureCollection(New List(Of KeyGesture) From {New KeyGesture(Key.F7, ModifierKeys.Alt)})
        )
    End Module
End Namespace
mm8
  • 163,881
  • 10
  • 57
  • 88
  • When I align both namespace names, I get a new error : **CustomCommands is not supported in a Windows Presentation Foundation (WPF) project"** – bluefox May 08 '18 at 13:19