如何在 Windows 通用应用程序 (Win10) 中基于默认样式创建样式?

How do I base a style on the default style in Windows Universal apps (Win10)?

在 WPF 中,如果要使样式基于控件的默认样式,您会说:

<Style TargetType="customControls:ResponsiveGridView" BasedOn="{StaticResource {x:Type GridView}}">

但是,UAP 不支持 x:Type - 我该怎么做?我尝试了以下 - none 有效(在将 XAML 定义为 GridView 所在的命名空间的别名之后)。

<Style TargetType="customControls:ResponsiveGridView" BasedOn="{StaticResource xaml:GridView}">

<Style TargetType="customControls:ResponsiveGridView" BasedOn="xaml:GridView">

None 有效 - 解析 XAML 时崩溃。

还有什么想法吗?

您仍然可以使用 "BasedOn" 来继承样式。

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

        <Style TargetType="Button" BasedOn="{StaticResource MyOtherStyle}" >
            <Setter Value="Green" Property="Foreground"></Setter>
        </Style>
    </Page.Resources>

像上面那样定义资源即可。它们将应用于页面上的每个按钮。

<Button Content="Hello"></Button>

要基于控件的默认样式,请不要使用"BasedOn"。您通过在样式中指定 TargetType 隐式地基于控件的默认样式。

为了您的特殊情况更精确: 如果您想对基于内置控件默认样式的自定义控件使用(隐式)样式,请执行以下操作: 创建以内置控件类型为目标的自定义样式。像这样:

<Page.Resources>
        <Style TargetType="Grid"  x:Key="MyStyle1" >
            <Setter Property="Background" Value="Green"></Setter>
        </Style>
...

然后添加另一个以您的自定义控件类型为目标的样式,该样式基于您对内置控件的自定义样式。像这样:

...    
<Style TargetType="local:MyCustomGrid" BasedOn="{StaticResource MyStyle1}">
                <Setter Property="BorderBrush" Value="Black"></Setter>
                <Setter Property="BorderThickness" Value="4"></Setter>
            </Style>
        </Page.Resources>

您所有的 MyCustomGrid 控件都将隐式获得基于默认样式的样式。

所有标准网格将保留其默认样式,因为它们不会隐式获取样式,因为您在第一个样式中指定了 x:key 因此必须显式设置网格的样式。这说明了吗?