UWP/XAML:如何使用 BasedOn 从默认样式继承? [英] UWP/XAML: How to inherit from the default styles using BasedOn?

查看:32
本文介绍了UWP/XAML:如何使用 BasedOn 从默认样式继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

微软官方文章 状态:

修改默认系统样式

您应该尽可能使用来自 Windows 运行时默认 XAML 资源的样式.当您必须定义自己的样式时,请尽可能尝试将您的样式基于默认样式(使用前面解释过的基于样式,或者从编辑原始默认样式的副本开始).

You should use the styles that come from the Windows Runtime default XAML resources when you can. When you have to define your own styles, try to base your styles on the default ones when possible (using based-on styles as explained earlier, or start by editing a copy of the original default style).

我知道您可以从 MSDN 以从编辑原始副本开始".然而,这让我觉得非常丑陋和不雅,因为即使我只想添加一件事,我也几乎粘贴了 100 行.

I understand that you can copy and paste the default style from MSDN in order to "start by editing a copy of the original". However, that strikes me as very ugly and inelegant, as I'm pasting in almost a 100 lines even if I just want to add one thing.

我喜欢使用基于样式"的想法;通过引用包含所有默认样式,但据我所知,Microsoft 提供的原始默认样式是隐式的.鉴于它们没有引用它们的键,如何使用 BasedOn ?

I like the idea of "using based-on styles" to include all of the default style by reference, but from what I can tell the original default styles supplied by Microsoft are implicit. Given that they have no key to reference them by, how can BasedOn be used?

推荐答案

你说得对,BasedOn 不能使用默认样式,因为它们是隐式的.

You are right about that BasedOn won't work with default styles as they are implicit.

但是,如果您只是想编辑某些属性,则不必包含完整的样式代码.

However, you don't have to include the full style code if you simply want to edit some properties.

例如,下面的 Button 将继承 default 样式的所有内容,除了 Background 颜色被更改为 Red.

For example, the Button below will inherit everything from the default style except the Background color being changed to Red.

<Page.Resources>
    <Style x:Key="RedButtonStyle"
           TargetType="Button">
        <Setter Property="Background"
                Value="Red" />
    </Style>
</Page.Resources>

<Grid>
    <Button Content="Red" Style="{StaticResource RedButtonStyle}" />
</Grid>

这篇关于UWP/XAML:如何使用 BasedOn 从默认样式继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆