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

WPF自定义控件库合并资源字典引用失败问题排查与解决

WPF自定义控件库合并资源字典引用失败问题解决

问题场景

我有一个结构如下的WPF自定义控件库项目,希望在另一个WPF应用中引用其中的合并资源字典:

CustomUI/
├── CustomStyle/
│   ├── BasicComponents/
│       ├── Buttons/
│       │   ├── SmallButton.xaml
│       │   └── StandardButton.xaml
│       ├── ComboBox/
│       │   └── StandardComboBox.xaml
│       ├── PasswordBox/
│       └── StandardPasswordBox.xaml
├── MergedResourceDictionary.xaml

已在MergedResourceDictionary.xaml中合并了4个资源字典,代码如下:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="CustomStyle/BasicComponents/Buttons/SmallButton.xaml"/>
    <ResourceDictionary Source="CustomStyle/BasicComponents/Buttons/StandardButton.xaml"/>
    <ResourceDictionary Source="CustomStyle/BasicComponents/ComboBox/StandardComboBox.xaml"/>
    <ResourceDictionary Source="CustomStyle/BasicComponents/PasswordBox/StandardPasswordBox.xaml"/>
</ResourceDictionary.MergedDictionaries>

在另一WPF应用的App.xaml中引用该合并字典时,提示找不到资源:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/CustomUI;component/MergedResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

但直接引用单个资源字典却能正常工作:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/CustomUI;component/CustomStyle/BasicComponents/Buttons/SmallButton.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

疑问:是否操作有误?比如是否不允许通过这种方式引用合并字典?


解决过程及经验总结

重建两个项目并修复命名空间问题后,问题得以解决。

具体细节:我习惯将C#类放在Scripts文件夹中,导致类使用CustomUI.Scripts命名空间而非根命名空间CustomUI,这引发了XAML中“clr-namespace未声明”的问题。在根目录创建使用CustomUI根命名空间的C#类后,XAML系统立即恢复正常。

推测:在pack路径pack://application:,,,/xxxx;component/yyyy.xaml中,xxxx对应的是应用的根命名空间,而非项目名称,供大家参考。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 22:16:11