【问题标题】:WPF: Global resource accessible by keyWPF:可通过键访问的全局资源
【发布时间】:2018-12-02 12:10:37
【问题描述】:

我使用如下资源将 Grid 控件设置为表格标题:

<Grid.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="Padding" Value="5,10" />
        <Setter Property="Foreground" Value="{StaticResource ForegroundDarkBrush}" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="HorizontalAlignment" Value="Center" />
    </Style>
    <Style TargetType="Border">
        <Setter Property="BorderThickness" Value="0.0,1.0,0.0,0" />
        <Setter Property="BorderBrush" Value="{StaticResource ForegroundDarkBrush}" />
        <Setter Property="Background" Value="{StaticResource BackgroundLightBrush}" />
    </Style>
</Grid.Resources>

问题是,我需要将这些资源应用到我的应用中的多个位置,这会导致代码重复。

我想知道这是否可以将资源存储在我的App.xaml 中并通过密钥或类似的方式使用它们?像这样:

<Resources Key="MyResourceSet">
     <Style>
         [..]
     </Style>
</Resources>

<Grid Resource="MyResourceSet">
[...]
</Grid>

【问题讨论】:

  • 你试过了吗?你绝对可以。查找资源时,它从第一个可用资源开始向上查找,因此 App.xaml 是它查找资源的最后一个位置。示例... TextBlock、Button、Grid、Window、App... 如果使用 TextBlock 中的资源,它会先在那里向外寻找,就像示例一样。
  • @MichaelPuckettII 我不能通过键/名称使用它们。这是不可能的。
  • 有可能,我一直这样做。
  • 我相信您现在可能会问一些不同的问题,因为我正在摸不着头脑。您想将资源字典应用于您的各个控件。你也可以这样做,但不是你期望的那样,我不相信。
  • @MichaelPuckettII 非常感谢您的努力! :-)

标签: c# wpf xaml resources styles


【解决方案1】:

将样式放置在 App.Resources 中,就像在任何其他 UIElement 中一样。

<Application x:Class="Question_Answer_WPF_App.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

        <Style x:Key="MyButtonStyle"
               TargetType="Button">
            <Setter Property="Background"
                    Value="Green" />
            <Setter Property="Height"
                    Value="30" />
            <Setter Property="Width"
                    Value="100" />
        </Style>

    </Application.Resources>
</Application>

在您的应用中任意引用。

<Window x:Class="Question_Answer_WPF_App.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="450"
        Width="800">

    <Button Content="Testing"
            Style="{StaticResource MyButtonStyle}" />

</Window>

如果您希望在您的应用程序中使用多个ResourceDictionary's,另一种方法是;但是使用相同的内部键,是引用将使用它的每个元素的唯一 ResourceDictionary。这不会使用App.xaml 资源,而是直接指向应用程序中的文件位置。由于ResourceDictionary's 有一个默认的“页面”“构建操作”,它将以这种方式引用该位置。如果您的ResourceDictionary 不能以这种方式工作,首先要做的是通过右键单击解决方案资源管理器中的ResourceDictionary 来检查它,并确保它是正确的。

示例:

MyCustomResourcesA.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="TextBlock">
        <Setter Property="FontSize"
                Value="46" />
    </Style>

    <Style x:Key="MyButtonStyle"
           TargetType="Button">
        <Setter Property="Background"
                Value="Green" />
        <Setter Property="Height"
                Value="30" />
        <Setter Property="Width"
                Value="100" />
    </Style>

</ResourceDictionary>

MyCustomResourcesB.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:Question_Answer_WPF_App">

    <Style TargetType="TextBlock">
        <Setter Property="FontSize"
                Value="26" />
    </Style>

    <Style x:Key="MyButtonStyle"
           TargetType="Button">
        <Setter Property="Background"
                Value="Blue" />
        <Setter Property="Height"
                Value="20" />
        <Setter Property="Width"
                Value="200" />
    </Style>

</ResourceDictionary>

MainWindow.xaml

<Window x:Class="Question_Answer_WPF_App.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="450"
        Width="800">

    <StackPanel HorizontalAlignment="Left">

        <StackPanel>
            <StackPanel.Resources>
                <ResourceDictionary Source="MyCustomResourcesA.xaml" />
            </StackPanel.Resources>
            <TextBlock Text="I'm using MyCustomResourcesA" />
            <Button Content="Testing"
                    Style="{StaticResource MyButtonStyle}" />
        </StackPanel>

        <StackPanel>
            <StackPanel.Resources>
                <ResourceDictionary Source="MyCustomResourcesB.xaml" />
            </StackPanel.Resources>
            <TextBlock Text="I'm using MyCustomResourcesB" />
            <Button Content="Testing"
                    Style="{StaticResource MyButtonStyle}" />
        </StackPanel>

    </StackPanel>

</Window>

看起来像:

【讨论】:

  • 这就是我要找的! :-) 谢谢
  • @Rusco NP,对最初的混乱表示歉意。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-08
  • 1970-01-01
  • 1970-01-01
  • 2011-11-16
  • 1970-01-01
  • 2018-03-26
相关资源
最近更新 更多