12

在 Web 开发中,样式表非常常用。在 Swing 中有用于处理 GUI 的布局管理器。我对 XAML 应用这些范例之一的假设是否正确?两个都?在这种情况下首选哪一个?

我已经检查过 Intellisense,但除了Style字段外,我没有发现任何特别明显的东西,而且我不清楚谷歌搜索的关键字是什么。建议?

4

2 回答 2

12

您正在寻找的是 ResourceDictionary。这比仅仅将样式放在 App.Resources 元素中要灵活得多,并且可以让您更好地控制样式的范围。

将样式放入 App.Resources 有许多缺点:

  • 它可以很快填满并变成一个庞大而臃肿的列表
  • 那里的每一种风格都可以在全球范围内使用。你可能不想要那个。

使用 A ResourceDictionary 在很大程度上解决了这个问题:

  • 样式可以保存在一个或多个程序集中,并且可以跨应用程序重复使用
  • 通过包含(或不包含)资源字典,您可以控制将哪些样式添加到页面
  • 您可以以对您来说合乎逻辑的方式对样式和模板进行分组和组织

资源字典非常接近地映射到 CSS 文件。基本上,您使用这些来存储混合的东西:

  • 风格
  • 控制模板和数据模板
  • 刷子等

而且,与样式表一样,您可以将它们应用于整个控件类型或使用命名样式的控件

<UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Project.Ui;component/Styles/DialogStyles.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/Project.Ui;component/Icons/Error.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/Project.Ui;component/Icons/Exit.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/Project.Ui;component/Icons/Warning.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
</UserControl.Resources>

定义资源字典:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
                        xmlns:sys="clr-namespace:System;assembly=mscorlib"
                        xmlns:Infrastructure="clr-namespace:Hsbc.Ice.Shell.Infrastructure"
                        xmlns:Ui="clr-namespace:Hsbc.Ice.Shell.Infrastructure.Ui">

        <LinearGradientBrush x:Key="{x:Static Ui:Brushes.SelectedRowBackgroundBrushKey}" StartPoint="0.5,0" EndPoint="0.5,1" 
                                 po:Freeze="True">
            <GradientStop Color="#4D5F6E96" Offset="0"/>
            <GradientStop Color="#2191A0BE" Offset="0.2"/>
            <GradientStop Color="#2191A0BE" Offset="0.45"/>
            <GradientStop Color="#745F6E96" Offset="1"/>
        </LinearGradientBrush>
    </ResourceDictionary>
于 2012-11-08T11:01:19.210 回答
11

将样式作为资源存储在程序集中的更好方法,以便它可以在多个文件中作为 css 使用

您可以检查:Silverlight 和样式

另请检查:如何通过 Application.Resources 设置 Silverlight 控件样式

将这样的样式放在 Application.Xaml 文件中或为您创建新的

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             x:Class="AppResStyle.App"
             >
    <Application.Resources>
        <Style x:Key="ButtonStyle" TargetType="Button">
            <Setter Property="BorderBrush" Value="Green" />
            <Setter Property="Foreground" Value="Blue" />
     </Style>
    </Application.Resources>
</Application>

现在您可以在多个 uercontrol 中使用这样的方式为按钮分配样式

<UserControl x:Class="AppResStyle.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="130" Height="80">
    <Grid x:Name="LayoutRoot" Background="White">
        <Button Content="Button1" Height="75" Width="125" Style="{StaticResource ButtonStyle}" />
    </Grid>
</UserControl>
于 2012-11-08T08:22:38.413 回答