使用另一个程序集中的WPF样式

Mit*_*nen 3 c# wpf xaml

我已经搜寻了数小时来尝试解决我的问题的不同解决方案,但一直无法在此处或此处找到解决方案。

我有一个WPF自定义控件库,其中还包含一个主题.xaml文件,该文件具有要应用于控件的样式,但是将其链接为ResourceDictionary后,在修改控件的style属性时无法访问该样式。

这就是我的链接方式

<ResourceDictionary Source="pack://application:,,,/MADEV.WPFNotification;component/Themes/Dark.xaml"/>
Run Code Online (Sandbox Code Playgroud)

这是.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>
Run Code Online (Sandbox Code Playgroud)

我希望对此有所帮助

编辑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>
Run Code Online (Sandbox Code Playgroud)

ves*_*san 7

好的,我将发布最基本的实现,希望它将向您显示正确的方向。

首先是控件库,项目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>
Run Code Online (Sandbox Code Playgroud)

现在,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>
Run Code Online (Sandbox Code Playgroud)

最后,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>
Run Code Online (Sandbox Code Playgroud)

在App.xaml中引用资源字典将使其对WPF应用程序中的所有窗口/控件均可用。如果不希望这样,可以将代码从App.xaml移到特定的XAML文件。

结果如下:

在此处输入图片说明


Nik*_*ava 4

您需要移入<properties:Settings x:key=Settings>tag.Since <ResourceDictionary>,Application.Resources 需要 ResourceDictionary 的实例。