UWP 从 dark/light 字典中设置背景

UWP set background from dark/light dictionary

 var solcolor = (SolidColorBrush)Application.Current.Resources["PopUpsBackground"];
 this.Background = new SolidColorBrush(solcolor.Color);

我以编程方式设置了 ContentDialogs 的背景,但它从应用程序获取请求的主题颜色,但我需要获取我设置的颜色。我发现这个:

   dialog.RequestedTheme = (Window.Current.Content as FrameworkElement).RequestedTheme;

但现在我需要从我需要的字典中获取颜色(深色或浅色) 我也发现了这个:

Background="{Binding Source={ThemeResource PopUpsBackground}}"

但是也不行

UWP set background from dark/light dictionary

您需要在 Application.Resources 中设置 ThemeDictionaries,如下所示。自定义 ContentDialog 的样式将默认背景 属性 编辑为您的自定义值。详情请参考document.

<ResourceDictionary>
    <ResourceDictionary.ThemeDictionaries>
        <ResourceDictionary x:Key="Light">
            <SolidColorBrush x:Key="DialogColor" Color="Red" />
        </ResourceDictionary>
        <ResourceDictionary x:Key="Dark">
            <SolidColorBrush x:Key="DialogColor" Color="SeaGreen" />
        </ResourceDictionary>
    </ResourceDictionary.ThemeDictionaries>


    <Style TargetType="ContentDialog">
        <Setter Property="Foreground" Value="{ThemeResource ContentDialogForeground}" />
        <Setter Property="Background" Value="{ThemeResource DialogColor}" />
        <Setter Property="BorderBrush" Value="{ThemeResource ContentDialogBorderBrush}" />
        <Setter Property="IsTabStop" Value="False" /> 
    </Style>
</ResourceDictionary>

请注意,为使 ContentDialog 具有新样式效果,您需要在更改当前主题后重新启动您的应用。