You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

在WPF类库中引入MaterialDesignXamlToolkit及资源解析问题求助

解决WPF类库中使用MaterialDesignXamlToolkit资源无法解析的问题

嘿,我来帮你搞定这个问题!你遇到的核心问题是资源字典的嵌套写法导致资源没有被全局合并,再加上之前的文件找不到异常可能是小配置问题。咱们一步步来解决:

1. 先把MaterialDesign资源字典的写法改对

你之前在根ResourceDictionary里嵌套了一个带x:Key="123"的子字典,这就相当于把MaterialDesign的资源都“藏”在了这个命名项里,你的Page根本找不到这些资源!正确的写法应该直接在根字典里合并主题资源,不需要额外嵌套:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <!-- 引入MaterialDesign的基础主题 -->
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
        <!-- 引入你选的配色方案 -->
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

如果你之前移除嵌套时碰到FileNotFoundException,快检查这两点:

  • 确认MaterialDesignThemesMaterialDesignColors这两个NuGet包都已经正确安装到你的类库项目里(右键项目→管理NuGet包就能看到)
  • 把这个资源字典的生成操作设为Page(右键文件→属性→生成操作,选Page就行)

2. 在Page里正确合并资源

你之前直接用Source引用资源字典的方式不对,这样资源字典的内容不会被合并到Page的资源池中,Page自然找不到那些动态资源。得把它放到MergedDictionaries里:

<Page x:Class="YourNamespace.YourPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
      TextElement.Foreground="{DynamicResource MaterialDesignBody}"
      TextElement.FontWeight="Regular"
      TextElement.FontSize="13"
      TextOptions.TextFormattingMode="Ideal"
      TextOptions.TextRenderingMode="Auto"
      Background="{DynamicResource MaterialDesignPaper}"
      FontFamily="{DynamicResource MaterialDesignFont}">
    <Page.Resources>
        <ResourceDictionary>
            <!-- 把MaterialDesign的资源字典合并进来 -->
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/MyAssembly;component/ResourceDictionary/MaterialDesign.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <!-- 这里可以放你Page自己的其他资源 -->
        </ResourceDictionary>
    </Page.Resources>

    <!-- 这里写你的Page内容 -->
</Page>

注意哦:

  • /MyAssembly;component/ResourceDictionary/MaterialDesign.xaml里的MyAssembly换成你类库项目的实际程序集名称(去项目属性→应用程序里就能看到)
  • 一定要把资源字典放到Page.Resources下的MergedDictionaries里,这样里面的动态资源才能被Page的所有元素访问到

3. 要是还不行?试试这些排查步骤

如果还是提示资源找不到,别慌,试试这些:

  • 清理然后重建整个解决方案(右键解决方案→清理,再右键→生成)
  • 去项目的输出目录看看,确认MaterialDesignThemes.Wpf.dllMaterialDesignColors.dll都被复制过去了
  • 检查一下XAML里的资源名称有没有拼写错误,比如MaterialDesignBody的大小写,动态资源可是区分大小写的哦!

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

火山引擎 最新活动