UWP/XAML: 如何使用BasedOn继承默认样式?

UWP/XAML: How to inherit from the default styles using BasedOn?

微软官方article声明:

Modify the default system styles

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 行,即使我只想添加一件事。

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

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

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

例如,下面的 Button 将继承 默认 样式的所有内容,除了 Background 颜色更改为 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>