Hi @DotNET Fan ,
Welcome to Microsoft Q&A!
Currently, UWP does not have an API to effectively switch resource files on the code side. As a workaround, it is recommended that you use Application.Current.Resources.MergedDictionaries to read specific keys and modify the value.
App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<Color x:Key="PrimaryColor">Blue</Color>
<Color x:Key="SecondaryColor">SkyBlue</Color>
<SolidColorBrush x:Key="PrimaryBrush" Color="{StaticResource PrimaryColor}" />
<SolidColorBrush x:Key="SecondaryBrush" Color="{StaticResource SecondaryColor}" />
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
MainPage.xaml
<Grid Background="{StaticResource PrimaryBrush }" >
<Button Background="{StaticResource SecondaryBrush}" Width="100" Height="100" HorizontalAlignment="Center" VerticalAlignment="Center" CornerRadius="4">Click Me</Button>
<StackPanel Orientation="Vertical" Padding="10" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0 0 0 20">
<RadioButton Content="Bluish" Tapped="RadioButton_Tapped"></RadioButton>
<RadioButton Content="Reddish" Tapped="RadioButton_Tapped_1"></RadioButton>
</StackPanel>
</Grid>
MainPage.xaml.cs
private void RadioButton_Tapped(object sender, TappedRoutedEventArgs e)
{
//SetTheme("bluish");
if (Application.Current.Resources.MergedDictionaries[0].ContainsKey("PrimaryBrush"))
{
var brush = Application.Current.Resources.MergedDictionaries[0]["PrimaryBrush"] as SolidColorBrush;
if (brush != null)
{
brush.Color = Colors.Blue;
}
}
if (Application.Current.Resources.MergedDictionaries[0].ContainsKey("SecondaryBrush"))
{
var brush = Application.Current.Resources.MergedDictionaries[0]["SecondaryBrush"] as SolidColorBrush;
if (brush != null)
{
brush.Color = Colors.SkyBlue;
}
}
}
private void RadioButton_Tapped_1(object sender, TappedRoutedEventArgs e)
{
//SetTheme("reddish");
if (Application.Current.Resources.MergedDictionaries[0].ContainsKey("PrimaryBrush"))
{
var brush = Application.Current.Resources.MergedDictionaries[0]["PrimaryBrush"] as SolidColorBrush;
if (brush != null)
{
brush.Color = Colors.Red;
}
}
if (Application.Current.Resources.MergedDictionaries[0].ContainsKey("SecondaryBrush"))
{
var brush = Application.Current.Resources.MergedDictionaries[0]["SecondaryBrush"] as SolidColorBrush;
if (brush != null)
{
brush.Color = Colors.Green;
}
}
}
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.