I am just learning WPF and C#. I followed this tutorial but cannot assign my custom WPF command. I get the following error:
Schweregrad Code Beschreibung Projekt Datei Zeile Unterdrückungszustand Fehler XDG0008 Der Name "Commands" ist im Namespace "clr-namespace:TileMapEditor" nicht vorhanden. TileMapEditor C:\Entwickl\c#\TileMapEditor\TileMapEditor\MainWindow.xaml 13
But in my code behind in namspace TileMapEditor I definded a static class Commands. Why is it not found?
Here is my YAML:
<Window x:Class="TileMapEditor.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:TileMapEditor"
xmlns:self="clr-namespace:TileMapEditor"
mc:Ignorable="d"
Title="TileMapEditor" Height="450" Width="800">
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.New" CanExecute="NewCommand_CanExecute" Executed="NewCommand_Executed" />
<CommandBinding Command="ApplicationCommands.Open" CanExecute="OpenCommand_CanExecute" Executed="OpenCommand_Executed" />
<CommandBinding Command="self:Commands.Exit" CanExecute="OpenCommand_CanExecute" Executed="OpenCommand_Executed" />
</Window.CommandBindings>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Command="New" />
<MenuItem Command="Open" />
<Separator />
<MenuItem Command="Exit" />
</MenuItem>
<MenuItem Header="_Edit">
<MenuItem Command="Cut" />
<MenuItem Command="Copy" />
<MenuItem Command="Paste" />
</MenuItem>
</Menu>
<TextBox AcceptsReturn="True" Name="txtEditor" />
</DockPanel>
</Window>
And here my C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TileMapEditor
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void NewCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void NewCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
txtEditor.Text = "";
}
private void OpenCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void OpenCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
txtEditor.Text = "Open";
}
}
public static class Commands
{
public static readonly RoutedUICommand Exit = new RoutedUICommand
(
"Exit",
"Exit",
typeof(MainWindow),
new InputGestureCollection()
{
new KeyGesture(Key.F4, ModifierKeys.Alt)
}
);
//Define more commands here, just like the one above
}
}
What I am missing here?
The issue was not related to my code but to Visual Studio. It seems it didn't recognize all changes correctly and creates such missleading error messages. A simple rebuild does the job. Sorry to bother you. :-)