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

VS2022加载WPF ResourceDictionary出错,求排查方案

WPF资源字典加载失败排查与解决方案

问题背景

在VS2022开发C# WPF .NET窗口项目时,创建了包含Button样式的ResourceDictionary,但始终无法加载,报错**"An error occurred while finding the resource dictionary"**。已尝试多种路径配置(pack URI、相对路径等),也试过新建项目、重新包含文件,均无效。

解决方案结构

项目层级如下:

  • 解决方案 RevitAddinManager
    • 项目 RevitAddinManager
      • Assets 文件夹
        • RevitStyles.xaml(目标资源字典文件)
      • 引用资源的窗口XAML文件

当前资源字典引用代码

<Window.Resources>
    <CollectionViewSource x:Key="AddinCollectionViewSource" CollectionViewType="ListCollectionView"/>
    <ResourceDictionary x:Key="RevitStyles">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/RevitAddinManager;component/Assets/RevitStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

已尝试的路径写法

<!-- 写法1 -->
<ResourceDictionary x:Key="RevitStyles" Source="pack://application:,,,/RevitAddinManager;component/Assets/RevitStyles.xaml"/>

<!-- 写法2 -->
Source="/Assets/RevitStyles.xaml"

<!-- 写法3 -->
Source="/RevitAddinManager/Assets/RevitStyles.xaml"

ResourceDictionary内容

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="RevitButton" TargetType="{x:Type Button}">
        <Style.Resources>
            <SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/>
            <SolidColorBrush x:Key="Button.Static.Border" Color="#FFA0A0A0"/>
            <SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFE5F1FB"/>
            <SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF0078D6"/>
            <SolidColorBrush x:Key="Button.Focused.Background" Color="{Binding Color, Source={StaticResource Button.Static.Background}}"/>
            <SolidColorBrush x:Key="Button.Focused.Border" Color="{Binding Color, Source={DynamicResource Button.MouseOver.Border}}"/>
            <SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFCCE4F7"/>
            <SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF005499"/>
            <SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFC9C9C9"/>
            <SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFA0A0A0"/>
            <SolidColorBrush x:Key="Button.Disabled.Foreground" Color="DimGray"/>
        </Style.Resources>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/>
        <Setter Property="Foreground" Value="{Binding Forground}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border x:Name="Border"
                            Background="{TemplateBinding Background}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            BorderBrush="{TemplateBinding BorderBrush}">
                        <ContentPresenter Name="Content" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="{StaticResource Button.MouseOver.Background}"/>
                            <Setter Property="BorderBrush" Value="{StaticResource Button.MouseOver.Border}"/>
                        </Trigger>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="Background" Value="{StaticResource Button.Focused.Background}"/>
                            <Setter Property="BorderBrush" Value="{StaticResource Button.Focused.Border}"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" Value="{StaticResource Button.Pressed.Background}"/>
                            <Setter Property="BorderBrush" Value="{StaticResource Button.Pressed.Border}"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Background" Value="{StaticResource Button.Disabled.Background}"/>
                            <Setter Property="BorderBrush" Value="{StaticResource Button.Disabled.Border}"/>
                            <Setter Property="TextElement.Foreground" TargetName="Content" Value="{StaticResource Button.Disabled.Foreground}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

排查与解决步骤

1. 修正资源字典引用结构

当前写法中给外层ResourceDictionary添加了x:Key,会导致资源无法全局访问,同时可能引发加载逻辑错误。正确的合并方式如下:

<Window.Resources>
    <ResourceDictionary>
        <CollectionViewSource x:Key="AddinCollectionViewSource" CollectionViewType="ListCollectionView"/>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/RevitAddinManager;component/Assets/RevitStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

2. 检查资源文件属性

右键RevitStyles.xaml → 属性:

  • 确保生成操作设置为Resource(而非Content或None)
  • 确保复制到输出目录设置为不复制(资源会嵌入程序集,无需复制)

3. 验证路径与程序集匹配

  • 确认pack URI中的程序集名称RevitAddinManager和项目的程序集名称完全一致(右键项目 → 属性 → 应用程序 → 程序集名称)
  • 同一程序集内可简化为相对路径:
    <ResourceDictionary Source="Assets/RevitStyles.xaml"/>
    

4. 修正字典内的语法错误

注意到Foreground绑定存在拼写错误:{Binding Forground} → 改为{Binding Foreground}(少写了一个e),该错误虽不直接导致加载失败,但会影响样式生效。

5. 清理重建解决方案

  • 点击VS菜单 → 项目 → 清理解决方案
  • 生成 → 重建解决方案
  • 重启VS后重新运行项目

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 07:24:56