XAML中CSS的等价物

XAML中CSS的等价物,xaml,Xaml,在web开发中,样式表非常常用。在Swing中,有用于处理GUI的布局管理器。我认为XAML应用了其中一种范式,这一假设正确吗?二者都在这种情况下,首选哪一种 我检查了Intellisense,但除了样式字段外,我没有发现任何特别明显的内容,我也不清楚谷歌的关键词是什么。建议?将样式作为资源存储在程序集中的更好方法,以便它可以作为css在多个文件中使用 您可以检查: 同时检查: 在Application.Xaml文件中放入这样的样式,或者为您创建一个新的样式 <Application xm

在web开发中,样式表非常常用。在Swing中,有用于处理GUI的布局管理器。我认为XAML应用了其中一种范式,这一假设正确吗?二者都在这种情况下,首选哪一种


我检查了Intellisense,但除了
样式
字段外,我没有发现任何特别明显的内容,我也不清楚谷歌的关键词是什么。建议?

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

您可以检查:

同时检查:

在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>

现在,您可以在多个控件中利用这样的方式为按钮指定样式

<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>

您要查找的是ResourceDictionary。这比仅仅在App.Resources元素中添加样式灵活得多,并使您能够更好地控制样式的范围

在应用程序中添加样式。资源有许多缺点:

  • 它可以很快填满,变成一个庞大的、臃肿的列表
  • 那里的每种款式都是全球通用的。你可能不想那样
使用ResourceDictionary在很大程度上解决了这一问题:

  • 样式可以保存在一个或多个程序集中,并在应用程序之间重复使用
  • 通过包括resourcedictionaries(或不包括resourcedictionaries),您可以控制添加到页面的样式
  • 您可以以符合逻辑的方式对样式和模板进行分组和组织
资源字典与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>


我想我看到了它的工作原理。然而,我发现,据我所知,没有办法只指定所有组件的左边距。一个人应该怎么做?我将有一堆文本块,因此上边距需要逐渐增加,而左边距需要保持固定。有没有更聪明的方法来创建一组水平对齐的文本块?@Andyj听起来像XAML布局是完成这项工作的工具: