WPF(.NET 8)XAML无法解析命名空间、转换器和视图模型,即使这些类是公开的

人工智能 2026-07-10

我快要被这个项目折磨疯了,说实话也不知道还能试些什么。

我正在开发一个面向.NET 8的 WPF应用,结果出现大量XAML错误,例如:

XDG0008: The name "XYZ" does not exist in the namespace → XDG0008:名称 "XYZ" 在命名空间中不存在

XDG0001: The resource "XYZ" could not be resolved → XDG0001:无法解析资源 "XYZ"

MC3037: Missing XmlNamespace, Assembly or ClrNamespace → MC3037:缺少XmlNamespace、程序集或ClrNamespace

XDG0006: Cannot find type in clr-namespace → XDG0006:在clr-namespace中找不到类型

再加上一些随机的类型不兼容错误:

[Error (active) MC3037 Missing XmlNamespace, Assembly or ClrNamespace in Mapping instruction. Line 442 Position 61. AMV.App C:\Users\barto\source\AMV_project\AMV\AMV.App\Views\MainWindow.xaml 442 → [错误(活动)MC3037:在映射指令中缺少XmlNamespace、Assembly或 ClrNamespace。行442,位置61。AMV.App C:\Users\barto\source\AMV_project\AMV\AMV.App\Views\MainWindow.xaml 442

Error XDG-0001 The resource "RoleMargin" could not be resolved. AMV.App C:\Users\barto\source\AMV_project\AMV\AMV.App\Views\MainWindow.xaml 337 → 错误XDG-0001:无法解析资源 "RoleMargin"。AMV.App C:\Users\barto\source\AMV_project\AMV\AMV.App\Views\MainWindow.xaml 337

Error XDG-0001 The resource "InvBoolVis" could not be resolved. AMV.App C:\Users\barto\source\AMV_project\AMV\AMV.App\Views\MainWindow.xaml 439 → 错误XDG-0001:无法解析资源 "InvBoolVis"。AMV.App C:\Users\barto\source\AMV_project\AMV\AMV.App\Views\MainWindow.xaml 439

Error XDG-0001 The resource "InvBoolVis" could not be resolved. AMV.App C:\Users\barto\source\AMV_project\AMV\AMV.App\Views\MainWindow.xaml 519 → 错误XDG-0001:无法解析资源 "InvBoolVis"。AMV.App C:\Users\barto\source\AMV_project\AMV\AMV.App\Views\MainWindow.xaml 519

Error XDG-0001 The resource "CountVis" could not be resolved. AMV.App C:\Users\barto\source\AMV_project\AMV\AMV.App\Views\MainWindow.xaml 259 → 错误XDG-0001:无法解析资源 "CountVis"。AMV.App C:\Users\barto\source\AMV_project\AMV\AMV.App\Views\MainWindow.xaml 259

Error XDG-0001 The resource "CountVis" could not be resolved. AMV.App C:\Users\barto\source\AMV_project\AMV\AMV.App\Views\MainWindow.xaml 333 → 错误XDG-0001:无法解析资源 "CountVis"。AMV.App C:\Users\barto\source\AMV_project\AMV\AMV.App\Views\MainWindow.xaml 333

Error XDG-0001 The resource "BoolVis" has an incompatible type. AMV.App C:\Users\barto\source\AMV_project\AMV\AMV.App\Views\MainWindow.xaml 47 → 错误XDG-0001:资源 "BoolVis" 的类型不兼容。AMV.App C:\Users\barto\source\AMV_project\AMV\AMV.App\Views\MainWindow.xaml 47

Error XDG-0001 The resource "BoolVis" has an incompatible type. AMV.App C:\Users\barto\source\AMV_project\AMV\AMV.App\Views\MainWindow.xaml 204 → 错误XDG-0001:资源 "BoolVis" 的类型不兼容。AMV.App C:\Users\barto\source\AMV_project\AMV\AMV.App\Views\MainWindow.xaml 204

示例

The name "ZeroToVisibilityConverter" does not exist in the namespace "clr-namespace:AMV.Converters"
→ 名称 "ZeroToVisibilityConverter" 在命名空间 "clr-namespace:AMV.Converters" 中不存在

The name "MainViewModel" does not exist in the namespace "clr-namespace:AMV.ViewModels"
→ 名称 "MainViewModel" 在命名空间 "clr-namespace:AMV.ViewModels" 中不存在

The name "Conversation" does not exist in the namespace "clr-namespace:AMV.Models"
→ 名称 "Conversation" 在命名空间 "clr-namespace:AMV.Models" 中不存在

The resource "BoolVis" has an incompatible type → 资源 "BoolVis" 具有不兼容的类型

The resource "RoleMargin" could not be resolved → 资源 "RoleMargin" 无法解析

另外:

CS1631 Cannot yield a value in the body of a catch clause → CS1631:在catch块的主体中不能产出值

重要细节

  • ALL classes (converters, view models, models, etc.) are declared as public
  • 所有类(转换器、视图模型、模型等)都声明为 public
  • Namespaces in code match the folder structure
  • 代码中的命名空间与文件夹结构相匹配
  • I tried both: xmlns:conv="clr-namespace:AMV.Converters" and xmlns:conv="clr-namespace:AMV.Converters;assembly=AMV"
  • 我尝试了两种: xmlns:conv="clr-namespace:AMV.Converters"xmlns:conv="clr-namespace:AMV.Converters;assembly=AMV"
  • Project builds partially, but XAML designer and runtime both complain
  • 项目部分构建成功,但XAML设计器和运行时都在报错
  • Resources are defined (or at least should be) in ResourceDictionaries
  • 资源是在ResourceDictionaries中定义的(或至少应该如此)
  • I also use merged dictionaries (MessageTemplates.xaml etc.)
  • 我也使用了合并字典(如MessageTemplates.xaml等)

我已经尝试过的方式

  • Cleaning solution (dotnet clean)
  • 清理解决方案 (dotnet clean)
  • Rebuilding everything
  • 重新构建所有内容
  • Deleting bin and obj
  • 删除 binobj
  • Restarting Visual Studio
  • 重新启动Visual Studio
  • Checking namespace typos 10+ times
  • 检查命名空间拼写错误十多遍
  • Making sure classes are public
  • 确保类被声明为 public
  • Moving classes between projects
  • 将类在项目之间移动
  • Adding/removing assembly= in XAML namespaces
  • 在XAML命名空间中添加/移除 assembly=
  • Verifying Build Action (Page, Resource, etc.)
  • 验证构建操作(Page、Resource等)
  • Checking if files are included in .csproj
  • 检查文件是否包含在 .csproj
  • Recreating some converters from scratch
  • 从头重新创建了一些转换器
  • Swearing at it (did not help)
  • 咒骂它(没用)

怀疑点

目前我怀疑以下某一种原因:

  • .NET 8 + WPF的怪异之处
  • 断裂的项目引用
  • 程序集名称不匹配(AMV vs AMV.App?)
  • XAML看不到已编译的代码隐藏部分
  • ResourceDictionary未正确加载
  • 项目结构本质上存在问题

完整项目可在此处找到:https://github.com/skye7x/AMV-Ai

问题

是什么原因会导致XAML无法看到我自己命名空间中的任何类型,尽管:

  • 类确实存在
  • 并且是公开的
  • 命名空间正确
  • 项目可以(部分)构建

是否有什么明显我忽略了,还是这是一个更深层次的WPF/.NET问题?

现在我真的不知道该如何继续处理这个项目了。

解决方案

  1. 大多数错误与存在的 "AssemblyInfo.cs" 文件有关。请将其从你的项目中排除。

enter image description here

  1. 下一条错误是你这样定义了键名: enter image description here

但你尝试使用不同的键来访问它们: enter image description here

正确的引用方式应当是这样: enter image description here

如果你更喜欢使用更短的键,可以在声明资源时定义它们: enter image description here

  1. The "DatabaseService.cs" file does not contain the "using System.IO;" → 文件 "DatabaseService.cs" 中并不包含 "using System.IO;"

  2. The "OllamaService.StreamChatAsync(...)" method is implemented incorrectly. → "OllamaService.StreamChatAsync(...)" 方法实现不正确。

你的代码看起来是这样的: enter image description here

但它应该是这样的:

        HttpResponseMessage response;
        string? error;
        try
        {
            response = await _http.SendAsync(req, HttpCompletionOption.ResponseHeadersRead, ct);
            error = null;
        }
        catch (Exception ex)
        {
            error = $"\n\n⚠️ **Connection Error**: {ex.Message}\n\nMake sure Ollama is running: `ollama serve`";
            response = null!;
        }
        if (error is not null)
        {
            yield return error;
            yield break;
        }
  1. 我在你的代码中找不到你想在 "BoolGrid" 键下指定的转换器: enter image description here

为了测试将其替换为固定大小:

        <Grid Background="{StaticResource BgDeepBrush}">
            <Grid.ColumnDefinitions>
                <!-- Sidebar -->
                <ColumnDefinition Width="200"/>

结果:在进行这些修改后,项目开始正常构建并运行。

我已经创建了一个Pull Request,解决当前项目启动问题。所有修复都已包含。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章