'How to get an event from the application when the system theme changes?
Because the Theme Qualifier for iOS assets is not yet supported on Uno, I have to resort to a helper service in my project, to provide alternate paths to image which are created for dark mode. This however causes the assets URL to be evaluated once, and not when the system theme is changed while the app is running. Is there a way to get an event or method being called from the application when the system theme changes?
Solution 1:[1]
If you want some UI assets to change based on theme, you can use ThemeResource with ThemeDictionaries.
Example in an Uno project
Declaration of the theme resource in the theme dictionary
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Uno.Gallery.Views.Styles.Application">
<ResourceDictionary.ThemeDictionaries>
<!-- Light Theme -->
<ResourceDictionary x:Key="Light">
<x:String x:Key="UnoLogoImageSource">ms-appx:///Assets/UnoGalleryLogo_Light.png</x:String>
</ResourceDictionary>
<!-- Dark Theme -->
<ResourceDictionary x:Key="Dark">
<x:String x:Key="UnoLogoImageSource">ms-appx:///Assets/UnoGalleryLogo_Dark.png</x:String>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
Reference code: https://github.com/unoplatform/Uno.Gallery/blob/master/Uno.Gallery/Uno.Gallery.UWP/Views/Colors.xaml#L11
Usage in a page
<Image Source="{ThemeResource UnoLogoImageSource}" />
Reference code: https://github.com/unoplatform/Uno.Gallery/blob/45e490668890c3a9db1e28a5b3de2a61822b07ca/Uno.Gallery/Uno.Gallery.UWP/Views/Shell.xaml#L45
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Batesias |
