使用另一个程序集中的 WPF 样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32190066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a WPF style from another assembly
提问by Mitchell Van Manen
I have been googling for hours trying out different solutions to my problem but have not been able to find one there or on here.
我已经在谷歌上搜索了几个小时来尝试不同的解决方案来解决我的问题,但无法在那里或在这里找到一个。
I have a WPF Custom Control Library that also includes a theme .xaml file that has styles I want to apply to controls, however after linking it as a ResourceDictionary I am unable to access the style when modifying the style attribute of a control.
我有一个 WPF 自定义控件库,其中还包含一个主题 .xaml 文件,该文件具有我想要应用于控件的样式,但是在将其链接为 ResourceDictionary 后,我无法在修改控件的样式属性时访问该样式。
This is how I am linking it
这就是我链接它的方式
<ResourceDictionary Source="pack://application:,,,/MADEV.WPFNotification;component/Themes/Dark.xaml"/>
and this is the .xaml file content:
这是 .xaml 文件内容:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MADEV.WPFNotification">
<SolidColorBrush x:Key="BG"
Color="#FF464646" />
<SolidColorBrush x:Key="BG_Darker"
Color="#FF3A3A3A" />
<SolidColorBrush x:Key="FG"
Color="#FFC5C5C5" />
<Style x:Key="NotificationStyle"
TargetType="Window">
<Setter Property="Height"
Value="36" />
<Setter Property="Width"
Value="150" />
<Setter Property="ResizeMode"
Value="NoResize" />
<Setter Property="ShowInTaskbar"
Value="False" />
<Setter Property="Topmost"
Value="True" />
<Setter Property="Focusable"
Value="False" />
<Setter Property="IsTabStop"
Value="False" />
<Setter Property="WindowStyle"
Value="None" />
<Setter Property="Foreground"
Value="White" />
<Setter Property="Background"
Value="{StaticResource BG}" />
<Setter Property="AllowsTransparency"
Value="True" />
<Setter Property="IsHitTestVisible"
Value="False" />
<Setter Property="ShowActivated"
Value="False" />
</Style>
I would appreciate some help with this
我将不胜感激
Edit 1: Current App.xaml after vesan's answer, yet still not working:
编辑 1:在 vesan 回答后的当前 App.xaml,但仍然无法正常工作:
<Application x:Class="SimpleOSD.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SimpleOSD"
xmlns:properties="clr-namespace:SimpleOSD.Properties"
StartupUri="BackgroundProcess.xaml">
<Application.Resources>
<properties:Settings x:Key="Settings" />
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Pack://application:,,,/MADEV.WPFNotification;component/Themes/Dark.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
回答by vesan
OK, I'll just post the most basic implementation, which will hopefully show you the right direction.
好的,我只会发布最基本的实现,希望它会向您展示正确的方向。
First the control library, project WpfControlLibrary1, file Dictionary1.xaml:
首先是控件库,项目 WpfControlLibrary1,文件 Dictionary1.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="TestBrush" Color="LightBlue"></SolidColorBrush>
</ResourceDictionary>
Now the WPF application, WpfApplication1 (references the control library), file App.xaml:
现在是 WPF 应用程序,WpfApplication1(引用控件库),文件 App.xaml:
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/WpfControlLibrary1;component/Dictionary1.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
And finally, Window1.xaml:
最后,Window1.xaml:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="400" Width="600"
Background="{StaticResource TestBrush}">
</Window>
Referencing the resource dictionary in App.xaml will make it available to all windows/controls in your WPF application. If you don't want that, you can move the code from App.xaml to a specific XAML file.
引用 App.xaml 中的资源字典将使其可用于 WPF 应用程序中的所有窗口/控件。如果您不希望这样,您可以将代码从 App.xaml 移动到特定的 XAML 文件。
And here's the result:
结果如下:
回答by Nikita Shrivastava
You need to move <properties:Settings x:key=Settings>inside <ResourceDictionary>tag.Since, Application.Resources requires an instance of ResourceDictionary.
你需要<properties:Settings x:key=Settings>在<ResourceDictionary>tag里面移动。因为 Application.Resources 需要一个 ResourceDictionary 的实例。
回答by Tveitan
I know it's a long time since this question was asked, but I recently needed find out of this and ended up here. So I thought I should share my solution as well.
我知道自从提出这个问题以来已经有很长时间了,但我最近需要找出答案并最终来到这里。所以我想我也应该分享我的解决方案。
Lets say that you have two assemblies (in same solution).
假设您有两个程序集(在同一个解决方案中)。
- MyProject1
- MyProject1.UI
- 我的项目1
- 我的项目1.UI
Step 1.Make sure that your are referencing the MyProject1.UI in MyProject1.
步骤 1.确保您正在引用 MyProject1 中的 MyProject1.UI。
MyProject1-> References -> Add Reference... -> Projects -> MyProject1.UI
MyProject1-> 引用 -> 添加引用... -> 项目 -> MyProject1.UI
Step 2.Then in MyProject1.UI you have a folder called "Styles" with this Brush.xaml
第 2 步。然后在 MyProject1.UI 中,您有一个名为“Styles”的文件夹,其中包含此 Brush.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="NormalBackgroundBrush" Color="SteelBlue"/>
<SolidColorBrush x:Key="ErrorBackgroundBrush" Color="LightCoral"/>
</ResourceDictionary>
Step 3.Then you edit your App.xaml in MyProject1 from this:
第 3 步。然后你在 MyProject1 中编辑你的 App.xaml:
<Application x:Class="MyProject1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/Brushes.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
To this:
对此:
<Application x:Class="MyProject1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyProject1.UI;component/Styles/Brushes.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Step 4.Now you can use the brushes as StaticResourcein your MyProject1.UI like this:
第 4 步。现在您可以像这样在 MyProject1.UI 中使用笔刷作为StaticResource:
Background="{StaticResource NormalBackgroundBrush}"
or
或者
Background="{StaticResource ErrorBackgroundBrush}"
Hope it helps someone : )
希望它可以帮助某人:)


