即使属性不相同,UWP Global Resources也会被Local Resources覆盖

时间:2018-07-12 15:44:52

标签: xaml uwp uwp-xaml

我有一个在App.xaml中定义的应用程序资源。如果未定义本地资源,则将测试该资源适用于本地样式。因此,这不是“资源未初始化”之类的问题。

代码如下。

App.xaml

<Application
    ...omitted
    RequestedTheme="Light">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Style/DefaultTheme.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

DefaultTheme.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="Button">
        <Setter Property="FontSize" Value="50" />
        <Setter Property="Background" Value="Red" />
    </Style>
</ResourceDictionary>

MainPage.xaml

...omitted
<StackPanel Orientation="Horizontal">
    <StackPanel.Resources>
        <Style TargetType="Button">
            <Setter Property="Width" Value="100" />
            <Setter Property="VerticalAlignment" Value="Stretch" />
        </Style>
    </StackPanel.Resources>
    <Button Content="Hello" />
    <Button Content="Happy" />
    <Button Content="World" />
</StackPanel>

您可以从StackPanel的本地资源中看到这一点。仅在全局词典分配WidthVerticalAlignment时设置FontSizeBackground。没有矛盾。

我也在Xamarin Forms XAML中尝试了这种语法。当本地资源分配相同的属性时,它们只会覆盖全局资源。如果未指定,则全局资源仍适用于该元素。

有关更多信息,ReSharper有一个漂亮的图标,上面写着“从DefaultTheme.xaml隐藏资源”。但是,当我在Xamarin Forms中执行此操作时,它只会淡出被覆盖的属性。

是否有办法使本地资源成为UWP中全局资源的补充?将x:Key分配给全局资源,然后添加BasedOn并不是解决方案。我认为让应用程序中的每个按钮不必获取全局资源在主题化方面都是有意义的。

0 个答案:

没有答案