[UWP] Change Lottie Animation Color / Theme Dynamically

Md. Niaz Mahmud 131 Reputation points
2024-09-30T07:38:02.08+00:00

I have used lottie json animation in xaml uwp using "CommunityToolkit.WinUI.Lottie" and "AnimatedVisualPlayer".

But for dark theme I can not see the lottie animation as the color is fixed. How can we dynamically change color of lottie animation?

<Page x:Class="Lottie_Test.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:Lottie_Test"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
      xmlns:lottie="using:CommunityToolkit.WinUI.Lottie"
      mc:Ignorable="d"
      Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid Background="White">
        <Border Background="Transparent"
                BorderThickness="3"
                BorderBrush="Transparent"
                Width="30"
                Height="30"
                HorizontalAlignment="Center"
                VerticalAlignment="Center">
            <muxc:AnimatedVisualPlayer x:Name="VoiceWaveLottiePlayer"
                                       AutomationProperties.Name="Lottie animation"
                                       AutoPlay="True">
                <lottie:LottieVisualSource x:Name="LottieJsonSource"
                                           Options="All"
                                           UriSource="ms-appx:///Assets/lottie_equalizer.json" />
            </muxc:AnimatedVisualPlayer>
        </Border>
    </Grid>
</Page>
Universal Windows Platform (UWP)
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
817 questions
{count} votes

Accepted answer
  1. Junjie Zhu - MSFT 19,556 Reputation points Microsoft Vendor
    2024-10-22T07:33:46.6166667+00:00

    Hi @Md. Niaz Mahmud ,

    Let me summarize this issue

    It is recommended to use Theme Listner to dynamically switch json files, then the UI will change with the theme.

    using Microsoft.Toolkit.Uwp.UI.Helpers;
     public MainPage()
     {
         this.InitializeComponent();
         var Listener = new ThemeListener();
         Listener.ThemeChanged += Listener_ThemeChanged; 
     }
     private void Listener_ThemeChanged(ThemeListener sender)
     {
         var theme = sender.CurrentTheme;
         if (theme == ApplicationTheme.Dark)
         {
             LottieJsonSource.UriSource = new System.Uri("ms-appx:///Assets/LightBulb.json");
         }
         else
         {
             LottieJsonSource.UriSource = new System.Uri("ms-appx:///Assets/LottieLogo1.json");
         }
     }
    
    

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.