[英]How to include MaterialDesignXamlToolkit to WPF class library?
我正在尝试在我的 WPF class 库(.NET 框架)中使用 MaterialDesignXamlToolkit。 我正在按照他们的官方快速入门教程进行操作,但由于我没有 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.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 的构建操作应该是页面,而且确实如此。
任何帮助将不胜感激!
现在我已经解决了这个问题,我意识到我的问题中缺少一个重要的信息:我遵循 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 加载材料设计库(第一行代码使用 MaterialDesignTheme.Wpf 和第二个 MaterialDesignColors),因为(我假设)它已经可以在编译时告诉需要这些库。
从您的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>
接受的解决方案对我有用。 不过,为了避免使用虚拟代码,我还可以通过将以下内容添加到资源字典的代码隐藏中来使 MDXT 工作:
Assembly.LoadFrom(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MaterialDesignThemes.Wpf.dll"));
Assembly.LoadFrom(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MaterialDesignColors.dll"));
不添加这些行。 仔细检查 MaterialDesign dll 文件是否被复制到应用程序的输出路径。
我以前见过这样的问题,只需添加无意义的代码,Visual Studio 就会意识到依赖于您的库的应用程序也依赖于 MaterialDesign 库,然后按照人们最初的预期再次复制 dll。
而不是添加这些行,你可以
此评论为我解决了问题,但请确保您没有其他错误,如果您刚刚找到它们并修复主题,请尝试运行该项目,它会工作。
using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
public MainWindow()
{
InitializeMaterialDesign();
InitializeComponent();
}
private void InitializeMaterialDesign()
{
// Create dummy objects to force the MaterialDesign assemblies to be loaded
// from this assembly, which causes the MaterialDesign assemblies to be searched
// relative to this assembly's path. Otherwise, the MaterialDesign assemblies
// are searched relative to Eclipse's path, so they're not found.
var card = new Card();
var hue = new Hue("Dummy", Colors.Black, Colors.White);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.