UWP set background from dark/light dictionary

135 Views Asked by At
 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

1

There are 1 best solutions below

0
On BEST ANSWER

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.