为什么 XAML 资源与 CSS 样式不同?

发布于 2024-07-13 11:49:58 字数 459 浏览 6 评论 0原文

在 Expression Blend 中,您可以创建一个 18 的字体大小,然后创建一个“字体大小资源”。

从 HTML/CSS 来看,我想不出何时需要为“字体大小”创建一种样式,为“字体样式”创建一种样式,为“字体粗细”创建一种样式。 相反,我想制作一种名为“CompanyHeader”的字体,并在其中设置 10 个不同的属性,例如字体粗细、字体样式、字体大小、颜色等。

为什么在 Expression Blend、XAML 中存在不同,为每个属性创建样式/资源有何意义?

此图显示了如何单击每个属性上的小按钮将其更改为资源: 替代文本 http://tanguay.info/web/external/blendStyles.png

In Expression Blend you can create a font-size of say 18 and then create a "font-size resource".

Coming at this from HTML/CSS, I cannot think of when I would want to make a style for a "font-size" and one for a "font-style" and one for a "font-weight". Instead I want to make a font called "CompanyHeader" and have 10 different attributes set in it, e.g. font-weight, font-style, font-size, color,etc.

Why is this different in Expression Blend, XAML, what is the sense of making a style/resource for each attribute?

this graphic shows how you can click on a little button on each attribute to change it into a resource:
alt text http://tanguay.info/web/external/blendStyles.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

泪冰清 2024-07-20 11:49:58

我没有使用 Blend 的经验,但 XAML 中的样式可以包含多个属性,甚至更多,因为与 css 不同,您只能将一种样式应用于一个元素,而不能组合多个单属性样式。

以下是设置多个属性的样式的示例:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <Page.Resources>
      <Style x:Key="MyStyle" TargetType="{x:Type Label}">
         <Setter Property="Width" Value="125"/>
         <Setter Property="Height" Value="25"/>
         <Setter Property="Background" Value="Red"/>
      </Style>
   </Page.Resources>
   <Label Style="{StaticResource MyStyle}"/>
</Page>

请注意,如果我想将样式分成 3 个较小的样式,每个样式设置一个属性,我就无法使用它们,因为 Label 的 Style 属性只能接受一种样式。

I have no experience with Blend, but styles in XAML can include more than one attribute, more then that, since unlike css you can only apply one style to an element you can't combine multiple one-attribute styles.

Here is an example for a style that set multiple properties:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <Page.Resources>
      <Style x:Key="MyStyle" TargetType="{x:Type Label}">
         <Setter Property="Width" Value="125"/>
         <Setter Property="Height" Value="25"/>
         <Setter Property="Background" Value="Red"/>
      </Style>
   </Page.Resources>
   <Label Style="{StaticResource MyStyle}"/>
</Page>

Note that if I wanted to break the style into 3 smaller styles each setting one property I couldn't use them because the Label's Style property can only accept one style.

二手情话 2024-07-20 11:49:58

我认为它们可能允许为 FontFamily、FontWeight 等创建单一资源,以允许它们在应用程序中的多种样式中使用。 通过在资源中放置单个属性,您可以立即使用该资源影响所有样式。 如果您没有使用资源,但尝试在整个应用程序(或其一部分)中使用一致的 FontFamily,那么您必须一次检查每种样式才能更新它。

为了在混合中创建具有多个属性的样式,您可以执行以下操作:

  • 选择要设置样式的控件(控件的类型将用作样式的 TargetType)
  • 从菜单中选择“对象”->“编辑样式”-> ;Create Empty
  • 输入您想要分配给样式的键(这是您将用来引用样式的名称)
  • 转到属性选项卡并开始应用您想要的该样式的外观/感觉

I think they likely allow for the creation of single resources for FontFamily, FontWeight, etc. to allow them to be used across many styles in the application. By placing a single property in a resource you can effect all styles using that resource at once. If you weren't using a resource but were attempting to use a consistent FontFamily across your whole application (or a portion of it) then you had to go through each style one at a time in order to update it.

In order to make a style with multiple properties in blend you can do the following:

  • Select the control you wish to style (the control's type will be used as the TargetType for the style)
  • From the menu select Object->Edit Style->Create Empty
  • Enter the Key you would like to assign to the style (this is the name that you will use to reference the style)
  • Go to the properties tab and begin to apply the look/feel that you want for that style
瞎闹 2024-07-20 11:49:58

它允许您在更细粒度的级别上重用字体详细信息,而不是必须定义字体定义的所有元素。 您可能有多种样式,它们都继承自相同的字体大小定义。 然后,您的设计人员可以更改字体大小,并且所有使用它的样式都会自动具有更新的外观。

It allows you to reuse the font details at am more granular level than having to define all elements of the font definition. You might have several styles that all inherit from the same font-size definition. Your designer can then change the font-size and all the styles that use it then automatically have an updated appearance.

飘过的浮云 2024-07-20 11:49:58

字体的每个实例都将使用自己的资源,如果多个此类定义引用相同的字体也没关系。

在两种不同的样式中使用相同的字体将最终得到该字体的两个实例(当应用样式时)。 Instade,您可以将字体定义为其自己的样式,并在其他样式中使用该样式。 在这种情况下,两种样式都会使用字体资源的一次运行时实例。

如果您有非常复杂的资源字典(例如您正在创建一个主题),那么最好将精细资产(如特定的颜色画笔或字体)定义为独立资源(或命名样式)并将其用于其他复杂的资源中。样式以节省系统资源。

关于 Blend 的问题,它简单地强制执行了这种最佳实践。

Each instance of a font will use its own resources, doesn't matter if multiple such defenitions refer to same font.

Using same font in two different styles will end-up with two instances of the font (when the styles are applied). Instade, you can defined the font as a style of its own and use the style with-in other styles. In this case, once runtime instance of the font resource is used by both styles.

If you have very complex resource dictionaries (say for example you are creating a theme), it is a good idea to define geanular assets (like a specific color brush or font) as independent resource (or named style) and use them in other complex styles to conserve system resources.

On your question on Blend, it simpley enforces this best practice.

梦年海沫深 2024-07-20 11:49:58

这可以通过创建一个辅助类来使用和包装您的样式来实现。 这里提到了CompoundStyle< /a> 展示了如何做到这一点。 有多种方法,但最简单的是执行以下操作:

<TextBlock Text="Test"
    local:CompoundStyle.StyleKeys="headerStyle,textForMessageStyle,centeredStyle"/>

博客文章讨论了 Win8 和 Windows Phone,但相同的代码也适用于 WPF(减去不需要的 Utilies 类)

希望有所帮助。

This is possible by creating a helper class to use and wrap your styles. CompoundStyle mentioned here shows how to do it. There are multiple ways, but the easiest is to do the following:

<TextBlock Text="Test"
    local:CompoundStyle.StyleKeys="headerStyle,textForMessageStyle,centeredStyle"/>

The blog post talks about Win8 and Windows Phone, but the same code works for WPF as well (minus the Utilies class which is not needed)

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文