Custom themes and dynamic UI styles for UWP app
Hello UWP experts ,
We have 6 resource dictionaries having different themes , particulary app background, button colors etc...
And initially from the application resources it loads the default one if user didn't select any other theme.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Themes/default.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
We have a content dialog where user can select a particular theme of their choice and the code will select the particular resource dictionary having that theme and loads the button colors and other background. But unfortunately UI is not getting updated with the background colors,styles and the application is crashing.
The resource dictionary has the same filename as the theme name and this is how it loads a particular theme xaml dynamically and it is done in
app.xaml.cs
var skinDictionary = new ResourceDictionary();
skinDictionary.Source = new Uri($"ms-appx:///Themes/{theme}.xaml", UriKind.Absolute);
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(skinDictionary);
The resource dictionary has button styles set with the key defined <Style TargetType="Button" x:Key="AppButtonStyle"> and it is being referred in button as Style="{ThemeResource AppButtonStyle}"
Also there are other windows created through a new dispatcher thread and the page uses the theme resources - for eg:
CoreApplicationView newView = CoreApplication.CreateNewView();
int newViewId = 0;
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Frame frame = new Frame();
frame.Navigate(typeof(NewPage));
}
What is the best approach to update the UI on the fly for custom themes defined in different resource dictionaries for these cases?