gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:55:06 31 4
gpt4 key购买 nike

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

1) 我使用 Nuget 安装了 MaterialDesignXamlToolkit。

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

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

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

最佳答案

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

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

关于c# - 如何将 MaterialDesignXamlToolkit 包含到 WPF 类库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55594443/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com