wpfvb.netrouted-commands

WPF command binding not working


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.


Solution

  • 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