99using Windows . Storage . Streams ;
1010using Windows . Storage ;
1111
12+ #if ! WINAPPSDK
13+ using DQ = Windows . System . DispatcherQueue ;
14+ using Windows . System ;
15+ #else
16+ using DQ = Microsoft . UI . Dispatching . DispatcherQueue ;
17+ using Microsoft . UI . Dispatching ;
18+ #endif
19+
1220namespace CommunityToolkit . Labs . WinUI . Rive ;
1321
1422/// <summary>
@@ -31,7 +39,7 @@ public sealed partial class RivePlayer
3139 private readonly ConcurrentQueue < Action > sceneActionsQueue = new ( ) ;
3240
3341 // Continuously invalidates the panel for repaint.
34- private AnimationTimer ? _animationTimer ;
42+ private DispatcherQueueTimer _animationTimer ;
3543
3644 // State machine inputs to set once the current async source load finishes.
3745 // The null-ness of this object also tells us whether an async load operation is currently running.
@@ -62,14 +70,26 @@ public RivePlayer()
6270 ( object s , PointerRoutedEventArgs e ) => HandlePointerEvent ( _scene . PointerMove , e ) ;
6371 this . PointerReleased +=
6472 ( object s , PointerRoutedEventArgs e ) => HandlePointerEvent ( _scene . PointerUp , e ) ;
73+
74+ _animationTimer = DQ . GetForCurrentThread ( ) . CreateTimer ( ) ;
75+ _animationTimer . Interval = TimeSpan . FromSeconds ( 1.0 / 120 ) ; // Refresh at 120fps.
76+ _animationTimer . Tick += AnimationTimer_Tick ;
6577 }
6678
67- private void OnXamlRootChanged ( bool isHostVisible )
79+ private void AnimationTimer_Tick ( DispatcherQueueTimer t , object e )
6880 {
69- if ( _animationTimer == null )
81+ if ( ! IsLoaded )
7082 {
71- return ;
83+ _animationTimer . Stop ( ) ;
84+ }
85+ else
86+ {
87+ InvalidateAnimation ( ) ;
7288 }
89+ }
90+
91+ private void OnXamlRootChanged ( bool isHostVisible )
92+ {
7393 if ( isHostVisible )
7494 {
7595 _animationTimer . Start ( ) ;
0 commit comments