[UWP] How to set ThemeResource in C#

  • Thread starter Thread starter Puelt
  • Start date Start date
P

Puelt

Guest
Hi! I make UWP app and I want to set ThemeResource in C#:

MessageBubbleThemes.xaml

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="MessageBubbleBackground" Color="#bdc3c7"/>
<SolidColorBrush x:Key="MessageBubbleForeground" Color="Black"/>
<SolidColorBrush x:Key="MyMessageBubbleBackground" Color="#3498db"/>
<SolidColorBrush x:Key="MyMessageBubbleForeground" Color="White"/>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<SolidColorBrush x:Key="MessageBubbleBackground" Color="#34495e"/>
<SolidColorBrush x:Key="MessageBubbleForeground" Color="White"/>
<SolidColorBrush x:Key="MyMessageBubbleBackground" Color="#27ae60"/>
<SolidColorBrush x:Key="MyMessageBubbleForeground" Color="White"/>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>


MessageBubble.xaml

<Grid x:Name="bubbleBody" Background="{ThemeResource MessageBubbleBackground}">
<RichTextBlock Margin="10" MaxWidth="300">
<Paragraph>
<Run x:Name="bubbleText"/>
</Paragraph>
</RichTextBlock>
</Grid>

MessageBubble.xaml.cs

private static ResourceDictionary _bubblesThemes = new ResourceDictionary()
{
Source = new System.Uri("ms-appx:///MessageBubbleThemes.xaml")
};

.......

bubbleBody.Background = (SolidColorBrush)_bubblesThemes["MyMessageBubbleBackground"];
bubbleText.Foreground = (SolidColorBrush)_bubblesThemes["MyMessageBubbleForeground"];

If I set Background as ThemeResource in XAML, then MessageBubble change his background on theme changed in Settings app, but if I set Background in C#, then MessageBubble don't change his background on theme changed.

How can I solve it?

Continue reading...
 
Back
Top