在WPF用户控件库中的UserControl之间共享资源的最简单方法是什么?

sed*_*vav 9 wpf user-controls resourcedictionary

有一个WPF用户控件库和两个(或更多)用户控件.我需要在两个用户控件中使用相同的样式.我该如何分享这种风格?例如:

这是风格:

<Style x:Key="customLabelStyle" TargetType="Label">
    ...
</Style>
Run Code Online (Sandbox Code Playgroud)

用户控制A:

<UserControl x:Class="Edu.Wpf.Example.UserControlA"
   ...xmlns stuff... >
   <Grid>
      ... some xaml markup...
      <Label Style="{StaticResource customLabelStyle}"/>
   </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

UserControl B:

 <UserControl x:Class="Edu.Wpf.Example.UserControlB"
   ...xmlns stuff... >
   <Grid>
      ... some another xaml markup...
      <Label Style="{StaticResource customLabelStyle}"/>
   </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

那么如何在不涉及应用程序app.xaml资源字典的情况下在库中的用户控件之间共享此样式?

UPDATE

我可以将Themes\Generic.xaml添加到我的库中并在那里定义样式.但在这种情况下,我必须使用ComponentResourceKey作为样式的关键.对?它很长而且不是很方便的表达......

Esp*_*dbø 14

假设您有一个定义颜色的资源,如下所示:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Color A="#FF" R="#FF" G="#22" B="#11" x:Key="MyRed"/>
    <Color A="#FF" R="#00" G="#FF" B="#21" x:Key="MyGreen"/>
    <Color A="#FF" R="#00" G="#22" B="#FF" x:Key="MyBlue" />


    <SolidColorBrush x:Key="MyGreenBrush" Color="{StaticResource MyGreen}"/>
    <SolidColorBrush x:Key="MyRedBrush" Color="{StaticResource MyRed}"/>
    <SolidColorBrush x:Key="MyBlueBrush" Color="{StaticResource MyBlue}"/>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

另一个定义了一些基本样式:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type TextBlock}" x:Key="PocTextBlock">
        <Setter Property="FontSize" Value="16"/>
    </Style>

    <Style TargetType="{x:Type TextBox}" x:Key="MyTextBox">
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="Foreground" Value="{DynamicResource MyGreenBrush}"/>
    </Style>

    <Style TargetType="{x:Type TextBlock}" x:Key="MyResultTextBlock">
        <Setter Property="FontSize" Value="16"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="Foreground" Value="{DynamicResource MyGreenBrush}"/>
    </Style>

    <Style TargetType="{x:Type Border}" x:Key="MyBorder">
        <Setter Property="BorderBrush" Value="{DynamicResource MyGreenBrush}"/>
        <Setter Property="BorderThickness" Value="4"/>
        <Setter Property="CornerRadius" Value="5"/>
    </Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

然后,您可以将资源添加到App.xaml的Application.Resources标记中,如下所示:

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="OtherStyles.xaml"/>
                <ResourceDictionary Source="Colors.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

然后,在所有UserControl中,您可以将样式或画笔用作StaticResources,如示例代码所示.

  • 这是一个插件库.所以我不能使用App.xaml. (3认同)
  • 只要它是插件库,我就不能在主应用程序中使用它的链接 (3认同)
  • 在添加ResourceDictionaries时,可以使用以下约定链接App.xaml中的其他模块:<ResourceDictionary Source ="pack:// application:,,,/<YourModule>; component/<YourFolder> /Colors.xaml"/> (2认同)

Cod*_*ked 2

您可以单独定义共享资源ResourceDictionary,然后使用MergedDictionariesUserControl将它们合并到您的资源中。