如何添加WPF自定义命令

1yjd4xko  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(127)

我正在学习WPF和C#。我遵循了this教程,但无法分配自定义WPF命令。我得到以下错误:
Schwergrad Code Beschreibung Projekt Datei Zeile Unterdrückungszustand Fehler XDG0008 Der Name“Commands”ist im pace“clr-namespace:TileMapEditor”nicht vorhanden. TileMapEditor C:\Entwickl\c#\TileMapEditor\TileMapEditor\MainWindow.xaml 13
但是在我的代码后面的namspace TileMapEditor我定义了一个静态类Commands。为什么找不到?
以下是我的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>

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

}

我错过了什么?

vxqlmq5t

vxqlmq5t1#

这个问题与我的代码无关,而是与Visual Studio有关。它似乎没有正确识别所有的更改,并创建这样的missleading错误消息。一个简单的重建完成了这项工作。抱歉打扰了:-)

相关问题