Class 库中的便携式 XAML 样式

Portable XAML Styles in a Class Library

所以我有一个应用程序,其样式直接放入 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>

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

(我只能 post 2 个链接,直到我获得更高的 Whosebug 声誉) 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>

刚刚发现这个 post。如果您使用的是Xamarin.Forms或.NET MAUI,您也可以这样做。

我在一个共享的 class 库项目中管理我的所有样式(与接受的答案中的步骤 1-4 相同)并且 添加 ResourceDictionary 如下所示。

<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:KlipperRemoteControl"
             xmlns:shared="clr-namespace:AndreasReitberger.Shared;assembly=SharedMauiXamlStylesLibrary"
             x:Class="KlipperRemoteControl.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <shared:DefaultTheme />
            </ResourceDictionary.MergedDictionaries>                
        </ResourceDictionary>
    </Application.Resources>
</Application>

DefaultTheme.xaml 建库

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="AndreasReitberger.Shared.DefaultTheme"
    
    xmlns:ios="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;assembly=Microsoft.Maui.Controls"
    >
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/Themes/Controls/BoxView.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Editor.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Entry.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Label.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Grid.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Frame.xaml" />
       
    </ResourceDictionary.MergedDictionaries>

    <Color x:Key="Transparent">Transparent</Color>
    <Color x:Key="TappedBackgroundColor">#eaeaea</Color>

    <Color x:Key="Green">#33AD79</Color>
    <Color x:Key="LightGreen">#38ef7d</Color>
    <Color x:Key="DarkGreen">#11998e</Color>
    <Color x:Key="Red">#ff4a4a</Color>
    <Color x:Key="DarkRed">#93291e</Color>
    <Color x:Key="Orange">#F78836</Color>
    <Color x:Key="DarkOrange">#F83017</Color>
    <Color x:Key="Blue">#3C8CF1</Color>
    <Color x:Key="LightBlue">#6dd5ed</Color>
    <Color x:Key="DarkBlue">#2193b0</Color>
    <Color x:Key="HyperLink">#567cd7</Color>
    <Color x:Key="White">#ffffff</Color>
    <Color x:Key="Black">#000000</Color>
    <Color x:Key="primary-lighter">#edcacd</Color>
    <Color x:Key="Liliac">#d483fc</Color>
    <Color x:Key="Purpleish-Blue">#5d4cf7</Color>
    <Color x:Key="Link">#567cd7</Color>
    <Color x:Key="Bright-Cyan">#3cdeff</Color>
    <Color x:Key="Lemon-Lime">#bdff27</Color>
    <Color x:Key="Yellow">#E9B31A</Color>
    <Color x:Key="Pink">#C6275C</Color>    
</ResourceDictionary>