From f84a747464ecc929def929d4af9744c30f02dea9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 24 Sep 2021 17:55:17 +0200 Subject: [PATCH 1/2] Fixed implicit animations reset by setting null --- .../Implicit.cs | 66 ++++++++++++------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Animations/Implicit.cs b/Microsoft.Toolkit.Uwp.UI.Animations/Implicit.cs index f24915bb385..0fc9a960489 100644 --- a/Microsoft.Toolkit.Uwp.UI.Animations/Implicit.cs +++ b/Microsoft.Toolkit.Uwp.UI.Animations/Implicit.cs @@ -53,7 +53,7 @@ public static ImplicitAnimationSet GetShowAnimations(UIElement element) if (collection is null) { - element.SetValue(ShowAnimationsProperty, collection = new()); + element.SetValue(ShowAnimationsProperty, collection = new ImplicitAnimationSet()); } return collection; @@ -80,7 +80,7 @@ public static ImplicitAnimationSet GetHideAnimations(UIElement element) if (collection is null) { - element.SetValue(HideAnimationsProperty, collection = new()); + element.SetValue(HideAnimationsProperty, collection = new ImplicitAnimationSet()); } return collection; @@ -107,7 +107,7 @@ public static ImplicitAnimationSet GetAnimations(UIElement element) if (collection is null) { - element.SetValue(AnimationsProperty, collection = new()); + element.SetValue(AnimationsProperty, collection = new ImplicitAnimationSet()); } return collection; @@ -145,15 +145,21 @@ static void OnAnimationsChanged(object sender, EventArgs e) oldCollection.AnimationsChanged -= OnAnimationsChanged; } - if (d is UIElement element && - e.NewValue is ImplicitAnimationSet collection) + if (d is UIElement element) { - collection.ParentReference = new(element); - collection.AnimationsChanged -= OnAnimationsChanged; - collection.AnimationsChanged += OnAnimationsChanged; + if (e.NewValue is ImplicitAnimationSet collection) + { + collection.ParentReference = new(element); + collection.AnimationsChanged -= OnAnimationsChanged; + collection.AnimationsChanged += OnAnimationsChanged; - ElementCompositionPreview.SetIsTranslationEnabled(element, true); - ElementCompositionPreview.SetImplicitShowAnimation(element, collection.GetCompositionAnimationGroup(element)); + ElementCompositionPreview.SetIsTranslationEnabled(element, true); + ElementCompositionPreview.SetImplicitShowAnimation(element, collection.GetCompositionAnimationGroup(element)); + } + else + { + ElementCompositionPreview.SetImplicitShowAnimation(element, null); + } } } @@ -179,15 +185,21 @@ static void OnAnimationsChanged(object sender, EventArgs e) oldCollection.AnimationsChanged -= OnAnimationsChanged; } - if (d is UIElement element && - e.NewValue is ImplicitAnimationSet collection) + if (d is UIElement element) { - collection.ParentReference = new(element); - collection.AnimationsChanged -= OnAnimationsChanged; - collection.AnimationsChanged += OnAnimationsChanged; + if (e.NewValue is ImplicitAnimationSet collection) + { + collection.ParentReference = new(element); + collection.AnimationsChanged -= OnAnimationsChanged; + collection.AnimationsChanged += OnAnimationsChanged; - ElementCompositionPreview.SetIsTranslationEnabled(element, true); - ElementCompositionPreview.SetImplicitHideAnimation(element, collection.GetCompositionAnimationGroup(element)); + ElementCompositionPreview.SetIsTranslationEnabled(element, true); + ElementCompositionPreview.SetImplicitHideAnimation(element, collection.GetCompositionAnimationGroup(element)); + } + else + { + ElementCompositionPreview.SetImplicitHideAnimation(element, null); + } } } @@ -213,15 +225,21 @@ static void OnAnimationsChanged(object sender, EventArgs e) oldCollection.AnimationsChanged -= OnAnimationsChanged; } - if (d is UIElement element && - e.NewValue is ImplicitAnimationSet collection) + if (d is UIElement element) { - collection.ParentReference = new(element); - collection.AnimationsChanged -= OnAnimationsChanged; - collection.AnimationsChanged += OnAnimationsChanged; + if (e.NewValue is ImplicitAnimationSet collection) + { + collection.ParentReference = new(element); + collection.AnimationsChanged -= OnAnimationsChanged; + collection.AnimationsChanged += OnAnimationsChanged; - ElementCompositionPreview.SetIsTranslationEnabled(element, true); - ElementCompositionPreview.GetElementVisual(element).ImplicitAnimations = collection.GetImplicitAnimationCollection(element); + ElementCompositionPreview.SetIsTranslationEnabled(element, true); + ElementCompositionPreview.GetElementVisual(element).ImplicitAnimations = collection.GetImplicitAnimationCollection(element); + } + else + { + ElementCompositionPreview.GetElementVisual(element).ImplicitAnimations = null; + } } } } From 4612a38f2a7dadfbe70eb2b9629b69c74381c151 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 24 Sep 2021 17:55:26 +0200 Subject: [PATCH 2/2] Added sample page demo for reset feature --- .../ImplicitAnimationsPage.xaml.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Implicit Animations/ImplicitAnimationsPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Implicit Animations/ImplicitAnimationsPage.xaml.cs index 4ca01d0139c..f038f8beef3 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Implicit Animations/ImplicitAnimationsPage.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Implicit Animations/ImplicitAnimationsPage.xaml.cs @@ -5,6 +5,7 @@ using System; using System.Numerics; using Microsoft.Toolkit.Uwp.UI; +using Microsoft.Toolkit.Uwp.UI.Animations; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Hosting; @@ -18,6 +19,8 @@ public sealed partial class ImplicitAnimationsPage : IXamlRenderListener { private Random _random = new Random(); private UIElement _element; + private ImplicitAnimationSet _animationSet; + private bool _areAnimationsToggled; public ImplicitAnimationsPage() { @@ -28,6 +31,8 @@ public ImplicitAnimationsPage() public void OnXamlRendered(FrameworkElement control) { _element = control.FindChild("Element"); + _animationSet = Implicit.GetAnimations(_element); + _areAnimationsToggled = true; } private void Load() @@ -60,6 +65,16 @@ private void Load() 1); } }); + + SampleController.Current.RegisterNewCommand("Toggle animations", (sender, args) => + { + if (_element != null) + { + Implicit.SetAnimations(_element, _areAnimationsToggled ? null : _animationSet); + + _areAnimationsToggled = !_areAnimationsToggled; + } + }); } } } \ No newline at end of file