在 WPF 用户控件库中使用 MaterialDesignInXamlToolkit
Posted
技术标签:
【中文标题】在 WPF 用户控件库中使用 MaterialDesignInXamlToolkit【英文标题】:Use MaterialDesignInXamlToolkit in a WPF UserControl library 【发布时间】:2021-09-25 02:01:39 【问题描述】:我想利用 MaterialDesignInXamlToolkit 创建一个 wpf UserControl 库。
显然,库没有快速入门指南建议的App.xaml
文件。我的印象是,我可以使用Themes\Generic.xaml
文件加上AssemblyInfo.cs
中的ThemeInfo
属性,但这不起作用(显然这仅适用于CustomControls)-它无法解析资源。
有没有办法让它在 wpf 类库中工作?
<Grid>
<Button Style="StaticResource MaterialDesignRaisedAccentButton"
Foreground="StaticResource SecondaryHueLightBrush"/>
</Grid>
【问题讨论】:
【参考方案1】:要么安装 NuGet 包并引用消费应用程序的 App.xaml
文件中的资源字典,要么安装包并在库中的控件本身中引用相同的资源字典:
<UserControl x:Class="WpfControlLibrary1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfControlLibrary1"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<ResourceDictionary>
<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>
</UserControl.Resources>
<Grid>
<Button Style="StaticResource MaterialDesignRaisedAccentButton"
Content="Test"/>
</Grid>
</UserControl>
确保在构建时将 MaterialDesignThemes.Wpf 和 MaterialDesignColors 程序集复制到应用的输出目录:
Dependent DLL is not getting copied to the build output folder in Visual Studio
【讨论】:
以上是关于在 WPF 用户控件库中使用 MaterialDesignInXamlToolkit的主要内容,如果未能解决你的问题,请参考以下文章