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

问题描述 投票:1回答:3

我正在尝试在我的WPF类库(.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)我的“主屏幕”是Page,所以我将资源添加到它:

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

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

<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的构建操作应该是页面,它是。

任何帮助将不胜感激!

c# wpf mvvm revit material-design-in-xaml
3个回答
1
投票

从你的<ResourceDictionary x:Key="123">中删除ResourceDictionary元素开头:

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

0
投票

现在我已经解决了这个问题,我发现我的问题中缺少一个重要的信息:我正在遵循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));

我不能强调这两行代码是完全废话(因为我不想将任何逻辑放在代码后面),但是否则不会加载库。此代码以某种方式'强制'Revit加载Material设计库(第一行代码使用MaterialDesignTheme.Wpf和第二个MaterialDesignColors),因为(我假设)它已经可以在编译时告诉那些库是必需的。


0
投票

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

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

而不是添加那些线

  1. 也可以直接在您的应用程序中参考MaterialDesign
  2. 使用构建事件确保将DLL复制到构建路径。
© www.soinside.com 2019 - 2024. All rights reserved.