c# - 使用来自另一个程序集的 WPF 样式

标签 c# wpf xaml

我已经在谷歌上搜索了几个小时,尝试用不同的解决方案来解决我的问题,但一直无法在那里或在这里找到一个。

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

我是这样连接的

<ResourceDictionary Source="pack://application:,,,/MADEV.WPFNotification;component/Themes/Dark.xaml"/>

这是 .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>

如果能帮上忙

编辑 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>

最佳答案

好的,我将只发布最基本的实现,希望它能为您指明正确的方向。

首先是控件库,项目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>

现在是 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>

最后,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>

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

结果如下:

enter image description here

关于c# - 使用来自另一个程序集的 WPF 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32190066/

相关文章:

c# - 在 WPF 中使用交互触发器

c# - WPF 更改边距将其他元素向下推

c# - 我从 GCM 服务器 {"success":1} But the Notification not Come to the Device 得到了这个响应

c# - ClickOnce list 文件 - 正在生成重复的 "file"条目

c# - Json.NET 基本类型 - 使用 Decimal 而不是 Double

wpf - MVVM针对同一对象的多个演示?

c# - SelectiveScrollingGrid 是否只卡住第一列的列?

WPF:使用 TemplateBinding 将整数绑定(bind)到 TextBlock

visual-studio-2010 - 如何在VS2010中为XAML禁用智能感知?

c# - 并发插入数据库