如何将 MaterialDesignXamlToolkit 包含到 WPF class 库中?

How to include MaterialDesignXamlToolkit to WPF class library?

我正在尝试在我的 WPF class 库(.NET 框架)中使用 MaterialDesignXamlToolkit。我正在关注他们的官方quick start tutorial,但由于我没有App.xaml,我不得不做一些调整。貌似有一步错了,不知道是哪一步

1) 我使用 Nuget 安装了 MaterialDesignXamlToolkit。

2) 我使用以下代码创建了 ResourceDictionary:(我指定了密钥,因为如果我不这样做就会出错)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary x:Key="123">
        <ResourceDictionary.MergedDictionaries>
            <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>
</ResourceDictionary>

如果我删除 <ResourceDictionary x:Key="123"> 元素,则会出现错误:

System.Windows.Markup.XamlParseException: Set property 'System.Windows.ResourceDictionary.Source' threw an exception.

FileNotFoundException: Could not load file or assembly 'MaterialDesignThemes.Wpf, Culture=neutral' or one of its dependencies.

3) 我的 'main screen' 是页面,所以我向其中添加了资源:

    <Page.Resources>
        <ResourceDictionary Source="/MyAsembly;component/ResourceDictionary/MaterialDesign.xaml" />
    </Page.Resources>

4)这里出现了明显的问题(这是官方教程的第二步):我在我的Page中添加如下代码:

<Page ...
      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}">

但我收到警告:The resource {MaterialDesignBody, MaterialDesignPaper, MaterialDesignFont} could not be resolved.

我尝试的一些解决方案指出 ResourceDictionary 的构建操作应该是页面,它确实是。

如有任何帮助,我们将不胜感激!

从您的 ResourceDictionary 中删除 <ResourceDictionary x:Key="123"> 元素以开始:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <ResourceDictionary.MergedDictionaries>
            <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>

然后您应该能够使用 属性 元素语法设置属性 您设置了 Resources 属性:

<Page ... 
      d:DesignHeight="450" d:DesignWidth="800">
    <Page.Resources>
        <ResourceDictionary Source="/MyAsembly;component/ResourceDictionary/MaterialDesign.xaml" />
    </Page.Resources>
    <Page.Background>
        <DynamicResource ResourceKey="MaterialDesignPaper" />
    </Page.Background>
</Page>

既然我已经解决了这个问题,我意识到我的问题中缺少一个重要信息:我正在遵循 MVVM 模式(所以我所有的代码隐藏文件都是空的)。

问题在于 Revit(我为其构建插件的应用程序)加载插件正在使用的库的方式。我仍然不明白它的内部逻辑,但是在第一页正在加载的代码后面添加以下两行解决了我的问题:

ColorZoneAssist.SetMode(new GroupBox(), ColorZoneMode.Accent);
Hue hue = new Hue("name", System.Windows.Media.Color.FromArgb(1, 2, 3, 4), System.Windows.Media.Color.FromArgb(1, 5, 6, 7));

我怎么强调这两行代码完全是胡说八道(因为我不想在代码后面放置任何逻辑),但是库不会以其他方式加载。此代码以某种方式 'forces' Revit 加载 Material 设计库(第一行代码使用 MaterialDesignTheme.Wpf,第二行使用 MaterialDesignColors),因为(我假设)它已经可以告诉在编译时需要这些库。

不添加这些行。 仔细检查 MaterialDesign dll 文件是否已复制到应用程序的输出路径。

我以前见过这样的问题,只是添加了无意义的代码,然后 Visual Studio 意识到你的应用程序依赖于你的库也依赖于 MaterialDesign 库,然后再次复制 dll,就像人们首先期望的那样。

您可以这样

而不是添加这些行
  1. 直接在您的应用程序中参考 MaterialDesign
  2. 使用构建事件确保 DLL 被复制到构建路径。

对我有用。不过,为了避免伪代码,我还可以通过将以下内容添加到资源字典的代码隐藏中来使 MDXT 正常工作:

Assembly.LoadFrom(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MaterialDesignThemes.Wpf.dll"));
Assembly.LoadFrom(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MaterialDesignColors.dll"));