Skip to content

Commit d7cf27f

Browse files
committed
Fixed crashes for shadow animations on unloaded items
1 parent 760ab9f commit d7cf27f

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

Microsoft.Toolkit.Uwp.UI.Animations/Xaml/Abstract/ShadowAnimation{TValue,TKeyFrame}.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,22 @@ public AnimationBuilder AppendToBuilder(AnimationBuilder builder, UIElement pare
5454
{
5555
static AnimationBuilder ThrowArgumentNullException()
5656
{
57-
throw new ArgumentNullException(
58-
"The target shadow cannot be animated at this time.");
57+
throw new ArgumentNullException("The target shadow cannot be animated at this time.");
5958
}
6059

6160
return ThrowArgumentNullException();
6261
}
6362

6463
if (Target is IAttachedShadow allShadows)
6564
{
66-
// in this case we'll animate all the shadows being used.
65+
// In this case we'll animate all the shadows being used
6766
foreach (var context in allShadows.EnumerateElementContexts()) //// TODO: Find better way!!!
6867
{
68+
if (context.Shadow is not DropShadow shadow)
69+
{
70+
continue;
71+
}
72+
6973
NormalizedKeyFrameAnimationBuilder<TKeyFrame>.Composition keyFrameBuilder = new(
7074
explicitTarget,
7175
Delay ?? delayHint ?? DefaultDelay,
@@ -75,25 +79,18 @@ static AnimationBuilder ThrowArgumentNullException()
7579

7680
AppendToBuilder(keyFrameBuilder, easingTypeHint, easingModeHint);
7781

78-
CompositionAnimation animation = keyFrameBuilder.GetAnimation(context.Shadow, out _);
82+
CompositionAnimation animation = keyFrameBuilder.GetAnimation(shadow, out _);
7983

80-
builder.ExternalAnimation(context.Shadow, animation);
84+
builder.ExternalAnimation(shadow, animation);
8185
}
8286

8387
return builder;
8488
}
85-
else
89+
else if (Effects.GetShadow((FrameworkElement)parent) is AttachedShadowBase shadowBase &&
90+
shadowBase.GetElementContext((FrameworkElement)parent).Shadow is DropShadow shadow)
8691
{
87-
var shadowBase = Effects.GetShadow(parent as FrameworkElement);
88-
if (shadowBase == null)
89-
{
90-
static AnimationBuilder ThrowArgumentNullException() => throw new ArgumentNullException("The target's shadow is null, make sure to set the Target property to an element with a Shadow");
91-
92-
return ThrowArgumentNullException();
93-
}
94-
95-
var shadow = shadowBase.GetElementContext((FrameworkElement)parent).Shadow;
96-
92+
// In this case, the animation is targeting the single shadow attached to the target element.
93+
// The same checks as before have been performed to ensure that unloading doesn't cause issues.
9794
NormalizedKeyFrameAnimationBuilder<TKeyFrame>.Composition keyFrameBuilder = new(
9895
explicitTarget,
9996
Delay ?? delayHint ?? DefaultDelay,
@@ -107,6 +104,8 @@ static AnimationBuilder ThrowArgumentNullException()
107104

108105
return builder.ExternalAnimation(shadow, animation);
109106
}
107+
108+
return builder;
110109
}
111110
}
112111
}

0 commit comments

Comments
 (0)