Skip to content

Commit fef65a5

Browse files
Merge pull request #4274 from Sergio0694/bugfix/implicit-animations-reset
Bugfix/implicit animations reset
2 parents d775ea4 + 4612a38 commit fef65a5

File tree

2 files changed

+57
-24
lines changed

2 files changed

+57
-24
lines changed

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Implicit Animations/ImplicitAnimationsPage.xaml.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Numerics;
77
using Microsoft.Toolkit.Uwp.UI;
8+
using Microsoft.Toolkit.Uwp.UI.Animations;
89
using Windows.UI.Xaml;
910
using Windows.UI.Xaml.Controls;
1011
using Windows.UI.Xaml.Hosting;
@@ -18,6 +19,8 @@ public sealed partial class ImplicitAnimationsPage : IXamlRenderListener
1819
{
1920
private Random _random = new Random();
2021
private UIElement _element;
22+
private ImplicitAnimationSet _animationSet;
23+
private bool _areAnimationsToggled;
2124

2225
public ImplicitAnimationsPage()
2326
{
@@ -28,6 +31,8 @@ public ImplicitAnimationsPage()
2831
public void OnXamlRendered(FrameworkElement control)
2932
{
3033
_element = control.FindChild("Element");
34+
_animationSet = Implicit.GetAnimations(_element);
35+
_areAnimationsToggled = true;
3136
}
3237

3338
private void Load()
@@ -60,6 +65,16 @@ private void Load()
6065
1);
6166
}
6267
});
68+
69+
SampleController.Current.RegisterNewCommand("Toggle animations", (sender, args) =>
70+
{
71+
if (_element != null)
72+
{
73+
Implicit.SetAnimations(_element, _areAnimationsToggled ? null : _animationSet);
74+
75+
_areAnimationsToggled = !_areAnimationsToggled;
76+
}
77+
});
6378
}
6479
}
6580
}

Microsoft.Toolkit.Uwp.UI.Animations/Implicit.cs

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static ImplicitAnimationSet GetShowAnimations(UIElement element)
5353

5454
if (collection is null)
5555
{
56-
element.SetValue(ShowAnimationsProperty, collection = new());
56+
element.SetValue(ShowAnimationsProperty, collection = new ImplicitAnimationSet());
5757
}
5858

5959
return collection;
@@ -80,7 +80,7 @@ public static ImplicitAnimationSet GetHideAnimations(UIElement element)
8080

8181
if (collection is null)
8282
{
83-
element.SetValue(HideAnimationsProperty, collection = new());
83+
element.SetValue(HideAnimationsProperty, collection = new ImplicitAnimationSet());
8484
}
8585

8686
return collection;
@@ -107,7 +107,7 @@ public static ImplicitAnimationSet GetAnimations(UIElement element)
107107

108108
if (collection is null)
109109
{
110-
element.SetValue(AnimationsProperty, collection = new());
110+
element.SetValue(AnimationsProperty, collection = new ImplicitAnimationSet());
111111
}
112112

113113
return collection;
@@ -145,15 +145,21 @@ static void OnAnimationsChanged(object sender, EventArgs e)
145145
oldCollection.AnimationsChanged -= OnAnimationsChanged;
146146
}
147147

148-
if (d is UIElement element &&
149-
e.NewValue is ImplicitAnimationSet collection)
148+
if (d is UIElement element)
150149
{
151-
collection.ParentReference = new(element);
152-
collection.AnimationsChanged -= OnAnimationsChanged;
153-
collection.AnimationsChanged += OnAnimationsChanged;
150+
if (e.NewValue is ImplicitAnimationSet collection)
151+
{
152+
collection.ParentReference = new(element);
153+
collection.AnimationsChanged -= OnAnimationsChanged;
154+
collection.AnimationsChanged += OnAnimationsChanged;
154155

155-
ElementCompositionPreview.SetIsTranslationEnabled(element, true);
156-
ElementCompositionPreview.SetImplicitShowAnimation(element, collection.GetCompositionAnimationGroup(element));
156+
ElementCompositionPreview.SetIsTranslationEnabled(element, true);
157+
ElementCompositionPreview.SetImplicitShowAnimation(element, collection.GetCompositionAnimationGroup(element));
158+
}
159+
else
160+
{
161+
ElementCompositionPreview.SetImplicitShowAnimation(element, null);
162+
}
157163
}
158164
}
159165

@@ -179,15 +185,21 @@ static void OnAnimationsChanged(object sender, EventArgs e)
179185
oldCollection.AnimationsChanged -= OnAnimationsChanged;
180186
}
181187

182-
if (d is UIElement element &&
183-
e.NewValue is ImplicitAnimationSet collection)
188+
if (d is UIElement element)
184189
{
185-
collection.ParentReference = new(element);
186-
collection.AnimationsChanged -= OnAnimationsChanged;
187-
collection.AnimationsChanged += OnAnimationsChanged;
190+
if (e.NewValue is ImplicitAnimationSet collection)
191+
{
192+
collection.ParentReference = new(element);
193+
collection.AnimationsChanged -= OnAnimationsChanged;
194+
collection.AnimationsChanged += OnAnimationsChanged;
188195

189-
ElementCompositionPreview.SetIsTranslationEnabled(element, true);
190-
ElementCompositionPreview.SetImplicitHideAnimation(element, collection.GetCompositionAnimationGroup(element));
196+
ElementCompositionPreview.SetIsTranslationEnabled(element, true);
197+
ElementCompositionPreview.SetImplicitHideAnimation(element, collection.GetCompositionAnimationGroup(element));
198+
}
199+
else
200+
{
201+
ElementCompositionPreview.SetImplicitHideAnimation(element, null);
202+
}
191203
}
192204
}
193205

@@ -213,15 +225,21 @@ static void OnAnimationsChanged(object sender, EventArgs e)
213225
oldCollection.AnimationsChanged -= OnAnimationsChanged;
214226
}
215227

216-
if (d is UIElement element &&
217-
e.NewValue is ImplicitAnimationSet collection)
228+
if (d is UIElement element)
218229
{
219-
collection.ParentReference = new(element);
220-
collection.AnimationsChanged -= OnAnimationsChanged;
221-
collection.AnimationsChanged += OnAnimationsChanged;
230+
if (e.NewValue is ImplicitAnimationSet collection)
231+
{
232+
collection.ParentReference = new(element);
233+
collection.AnimationsChanged -= OnAnimationsChanged;
234+
collection.AnimationsChanged += OnAnimationsChanged;
222235

223-
ElementCompositionPreview.SetIsTranslationEnabled(element, true);
224-
ElementCompositionPreview.GetElementVisual(element).ImplicitAnimations = collection.GetImplicitAnimationCollection(element);
236+
ElementCompositionPreview.SetIsTranslationEnabled(element, true);
237+
ElementCompositionPreview.GetElementVisual(element).ImplicitAnimations = collection.GetImplicitAnimationCollection(element);
238+
}
239+
else
240+
{
241+
ElementCompositionPreview.GetElementVisual(element).ImplicitAnimations = null;
242+
}
225243
}
226244
}
227245
}

0 commit comments

Comments
 (0)