简体   繁体   中英

UWP set background from dark/light dictionary

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

I set the Background of ContentDialogs programmatically but it gets the requested theme color from an application, but I need to get the color that I set. I find this:

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

But now I need to get color from the dictionary I need( dark or light) I also find this:

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

but it does not work either

UWP set background from dark/light dictionary

You need set ThemeDictionaries in the Application.Resources like following. And custom ContentDialog's style edit the default Background property as your custom value. For more detail please refer to this 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>

Please note, for making ContentDialog new style effect, you need restart your app after change current theme.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM