Skip to main content
 首页 » 编程设计

c#之类库中的可移植 XAML 样式

2024年02月06日15yxwkf

所以我有一个应用程序,其样式直接放入 App.xaml 文件中:

<Application x:Class="Test.App" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             Startup="OnStartup"> 
    <Application.Resources> 
        <Style x:Key="SpecialButtonStyle" TargetType="Button"> 
            <Setter Property="Content" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" /> 
            <Setter Property="OverridesDefaultStyle" Value="true"/> 
            <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" /> 
            <Setter Property="BorderThickness" Value="2" /> 
            <Setter Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" /> 
            <Setter Property="Foreground" Value="White" /> 
            <Setter Property="Block.Foreground" Value="White" /> 
            <Setter Property="TextBlock.Foreground" Value="White" /> 
            <Setter Property="TextElement.Foreground" Value="White" /> 
            <Setter Property="FontWeight" Value="Bold" /> 
            <Style.Triggers> 
                <Trigger Property="IsMouseOver" Value="False"> 
                    <Setter Property="Template"> 
                        <Setter.Value> 
                            <ControlTemplate TargetType="{x:Type Button}"> 
                                <Border Background="{TemplateBinding Background}" Padding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness}"> 
                                    <Border Background="{TemplateBinding BorderBrush}"> 
                                        <ContentControl Foreground="White"> 
                                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 
                                        </ContentControl> 
                                    </Border> 
                                </Border> 
                            </ControlTemplate> 
                        </Setter.Value> 
                    </Setter> 
                </Trigger> 
            </Style.Triggers> 
        </Style> 
    </Application.Resources> 
</Application> 

我想将其作为样式包含在我的类库中,以便引用该库的任何 xaml 项目都可以在设计器中将“SpecialButtonStyle”视为可选的“样式”。 我已经阅读了几篇关于 ResourceDictionaries 和创建可移植 XAML 控件的文章,但我仍然感到困惑。我基本上想要一个样式集合作为类库的一部分。

(我只能发布 2 个链接,直到我获得更高的 StackOverflow 声誉) http://timheuer.com/blog/archive/2012/03/07/creating-custom-controls-for-metro-style-apps.aspx

http://visualstudiomagazine.com/articles/2015/03/01/everyone-gets-xaml-with-xamarinforms.aspx

如有任何帮助,我们将不胜感激。谢谢!

请您参考如下方法:

你读到的是正确的。

您需要在共享程序集中创建一个具有给定名称的纯 ResourceDictionary。

在您的 App.xaml 中,您可以将此 ResourceDictionary 作为 MergedDictionary 包含在内,因此您的整个应用程序将可以访问所有共享词典资源。

步骤:

  1. 创建另一个项目 WPF 控件库项目(与用户或自定义无关)
  2. 在新项目中右击->添加->ResourceDictionary
  3. 粘贴您的样式,使其看起来如下所示:

Dictionary1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Style x:Key="SpecialButtonStyle" TargetType="Button"> 
        <Setter Property="Content" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" /> 
        <Setter Property="OverridesDefaultStyle" Value="true"/> 
        <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" /> 
        <Setter Property="BorderThickness" Value="2" /> 
        <Setter Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" /> 
        <Setter Property="Foreground" Value="White" /> 
        <Setter Property="Block.Foreground" Value="White" /> 
        <Setter Property="TextBlock.Foreground" Value="White" /> 
        <Setter Property="TextElement.Foreground" Value="White" /> 
        <Setter Property="FontWeight" Value="Bold" /> 
        <Style.Triggers> 
            <Trigger Property="IsMouseOver" Value="False"> 
                <Setter Property="Template"> 
                    <Setter.Value> 
                        <ControlTemplate TargetType="{x:Type Button}"> 
                            <Border Background="{TemplateBinding Background}" Padding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness}"> 
                                <Border Background="{TemplateBinding BorderBrush}"> 
                                    <ContentControl Foreground="White"> 
                                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 
                                    </ContentControl> 
                                </Border> 
                            </Border> 
                        </ControlTemplate> 
                    </Setter.Value> 
                </Setter> 
            </Trigger> 
        </Style.Triggers> 
    </Style> 
</ResourceDictionary> 
  1. 在您的主项目中引用这个新的控件库
  2. 在App.xaml中引用Dictionary1.xaml

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"  
                 StartupUri="NestedXamlObjects.xaml"> 
    <Application.Resources> 
        <ResourceDictionary> 
            <ResourceDictionary.MergedDictionaries> 
                <ResourceDictionary Source="pack://application:,,,/WpfControlLibrary1;component/Dictionary1.xaml" /> 
            </ResourceDictionary.MergedDictionaries> 
        </ResourceDictionary> 
    </Application.Resources> 
</Application>