相当于XAML中的CSS

9 xaml

在Web开发中,样式表非常常用.我记得在Swing中,有一些用于处理GUI的布局管理器.我是否认为XAML应用了这些范例之一?都?在这种情况下哪一个是优选的.

我已经检查了intellisense但接受了该Style字段,我没有发现任何特别明显的东西,我不清楚google的关键字.建议?

Fas*_*ons 11

您正在寻找的是ResourceDictionary.这比仅仅在App.Resources元素中添加样式更灵活,并且可以更好地控制样式的范围.

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

  • 它可以很快填满并变成一个庞大的,膨胀的列表
  • 每种风格都可以在全球范围内使 你可能不希望这样.

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

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

资源字典非常接近于CSS文件.基本上,你使用这些来存储混合的东西:

  • 样式
  • ControlTemplates和DataTemplates
  • 刷子等

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

<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>
Run Code Online (Sandbox Code Playgroud)

定义ResourceDictionary:

    <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>
Run Code Online (Sandbox Code Playgroud)


Pra*_*ana 8

将样式存储为程序集中的资源的更好方法,以便它可以作为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>
Run Code Online (Sandbox Code Playgroud)

现在你可以在多个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>
Run Code Online (Sandbox Code Playgroud)