如何将 MaterialDesignXamlToolkit 包含到 WPF 类库中? [英] How to include MaterialDesignXamlToolkit to WPF class library?

查看:154
本文介绍了如何将 MaterialDesignXamlToolkit 包含到 WPF 类库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 WPF 类库(.NET 框架)中使用 MaterialDesignXamlToolkit.我正在关注他们的官方快速入门教程,但因为我这样做没有 App.xaml,我不得不做一些调整.显然有些步骤是错误的,但我不知道是哪一步.

I'm trying to use MaterialDesignXamlToolkit in my WPF class library (.NET framework). I'm following their official quick start tutorial, but since i do not have App.xaml, i had to make some adjustments. Apperently some step was wrong, but i do not know which one.

1) 我使用 Nuget 安装了 MaterialDesignXamlToolkit.

1) I installed MaterialDesignXamlToolkit using Nuget.

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

2) I created ResourceDictionary with the following code: (i specified the key because there is an error if i don't)

<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"> 元素,则会出现错误:

If i remove <ResourceDictionary x:Key="123"> element, then i get an error:

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) 我的主屏幕"是页面,所以我添加了资源:

3) My 'main screen' is Page, so i added the resource to it:

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

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

4) The obvious problem occurs here (this is the second step of the official tutorial): i add the following code to my 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}">

但我收到一条警告:资源 {MaterialDesignBody, MaterialDesignPaper, MaterialDesignFont} 无法解析.

我尝试过的一些解决方案指出 ResourceDictionary 的构建操作应该是 page,它是.

Some of the solutions i tried pointed out that the ResourceDictionary's build action should be page, and it is.

任何帮助将不胜感激!

推荐答案

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

Now that i've solved the problem, i realize one important information is missing from my question: i was following MVVM pattern (so all my code behind files were empty).

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

The problem was with the way Revit (the application that i was building a plugin for) loads libraries that a plugin is using. I still do not understand the internal logic of it, but the following two lines added to the code behind of the first page what is being loaded solved the problem for me:

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));

我不能强调这两行代码是完全废话(因为我不想在代码后面放置任何逻辑),但不会加载库.这段代码以某种方式强制"Revit 加载材料设计库(第一行代码使用 MaterialDesignTheme.Wpf 和第二个 MaterialDesignColors),因为(我假设)它在编译时已经可以判断需要这些库.

I cannot stress enought that those two lines of code are a complite bullshit (since i do not want to place any logic to code behind), but the libraries won't otherwise be loaded. This code somehow 'forces' Revit to load Material design libraries (1st line of code uses MaterialDesignTheme.Wpf, and the 2nd MaterialDesignColors), since (i assume) it can already tell at compile time that those libraries are needed.

这篇关于如何将 MaterialDesignXamlToolkit 包含到 WPF 类库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆