You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF自定义命令无法识别:XDG0008错误排查求助

问题描述

我正在学习WPF和C#,实现自定义WPF命令时遇到以下错误:

严重程度 代码 描述 项目 文件 行 抑制状态
错误 XDG0008 命名空间“clr-namespace:TileMapEditor”中不存在名称“Commands”。 TileMapEditor C:\Entwickl\c#\TileMapEditor\TileMapEditor\MainWindow.xaml 13

但我已在TileMapEditor命名空间的后台代码中定义了静态类Commands,为何无法被识别?

XAML代码

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

        //在这里定义更多命令,如上所示
    }

}

请问我遗漏了什么?


解决方案

1. 先编译项目

XAML设计器依赖编译后的程序集识别后台代码中的类型。只编写C#代码但未编译时,XAML找不到Commands类是正常现象。先执行生成解决方案(快捷键Ctrl+Shift+B),再查看错误是否消失。

2. 修复Exit菜单项的绑定

XAML中File菜单下的Exit菜单项写的是<MenuItem Command="Exit" />,这里的Exit未指定命名空间,WPF会默认查找系统命令,但你的Exit是自定义命令,必须修改为:

<MenuItem Command="self:Commands.Exit" />

否则即使解决了前面的错误,该菜单项也无法正确绑定到自定义命令。

3. 清理并重建项目(编译后仍有问题时)

Visual Studio的缓存可能导致类型识别异常,执行以下步骤:

  • 点击菜单生成 -> 清理解决方案
  • 删除项目目录下的bin和obj文件夹
  • 重新生成解决方案
  • 重启Visual Studio

4. 确认命名空间与程序集的一致性

虽然你的Commands类确实在TileMapEditor命名空间下,但如果项目程序集名称与命名空间不一致,需要在XAML的命名空间声明中添加程序集名称,例如:

xmlns:self="clr-namespace:TileMapEditor;assembly=TileMapEditor"

同一项目下通常无需添加,但如果修改过程序集名称,需调整此设置。


内容的提问来源于stack exchange,提问作者Thorsten Erdmann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 17:43:20