I have a .gif icon which I am trying to set using Lottie in UWP. The icon has two versions for light and dark mode respectively. But I cant change the icon when application theme is changed. I have followed this way,
Created a theme dictionary using this,
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:muxc="using:Microsoft.UI.Xaml.Controls" xmlns:lottie="using:Microsoft.Toolkit.Uwp.UI.Lottie"> <ResourceDictionary> <ResourceDictionary.ThemeDictionaries> <ResourceDictionary x:Key="Light"> <Style x:Key="AnimationStyle" TargetType="lottie:LottieVisualSource"> <Setter Property="UriSource" Value="/Assets/Icons/theme-light/lottie_animation.json" /> </Style> </ResourceDictionary> <ResourceDictionary x:Key="Dark"> <Style x:Key="AnimationStyle" TargetType="lottie:LottieVisualSource"> <Setter Property="UriSource" Value="/Assets/Icons/theme-dark/lottie_animation.json" /> </Style> </ResourceDictionary> </ResourceDictionary.ThemeDictionaries> </ResourceDictionary>After that I have called it following this,
<Grid.Row="3">
<muxc:AnimatedVisualPlayer HorizontalAlignment="Center"
VerticalAlignment="Center"
Stretch="Uniform">
<lottie:LottieVisualSource UriSource="{ThemeResource AnimationStyle}"/>
</muxc:AnimatedVisualPlayer>
</Grid>
But it does not show in both theme (Dark and Light). Please help :(
First of all, you could just save the string value for the
UriSource. Then I tested this, the result shows that theLottieVisualSourcewill not use the updated source when the Theme is changed. So we need to use the Theme Listener to detect the theme change and manually update theUriSource.Here is the code that I'm using for testing.
MainPage.Xaml
MainPage.Xaml.cs