private async void SlideIn(ImageLoadedTransitionTypes transitionType) { _image.Opacity = _targetOpacity; // Built-in animations are nice, but not very customizable. Leaving this for posterity. ////var animation = new RepositionThemeAnimation ////{ //// FromVerticalOffset = _image.ActualHeight, //// Duration = TimeSpan.FromSeconds(2) ////}; ////Storyboard.SetTarget(animation, _image); var oldTransform = _image.RenderTransform; var tempTransform = new TranslateTransform(); _image.RenderTransform = tempTransform; DoubleAnimation animation = null; switch (transitionType) { case ImageLoadedTransitionTypes.SlideUp: animation = new DoubleAnimation { From = _image.ActualHeight, To = 0, Duration = TimeSpan.FromSeconds(1), EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut } }; Storyboard.SetTargetProperty(animation, "Y"); break; case ImageLoadedTransitionTypes.SlideDown: animation = new DoubleAnimation { From = -_image.ActualHeight, To = 0, Duration = TimeSpan.FromSeconds(1), EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut } }; Storyboard.SetTargetProperty(animation, "Y"); break; case ImageLoadedTransitionTypes.SlideRight: animation = new DoubleAnimation { From = -_image.ActualWidth, To = 0, Duration = TimeSpan.FromSeconds(1), EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut } }; Storyboard.SetTargetProperty(animation, "X"); break; case ImageLoadedTransitionTypes.SlideLeft: animation = new DoubleAnimation { From = _image.ActualWidth, To = 0, Duration = TimeSpan.FromSeconds(1), EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut } }; Storyboard.SetTargetProperty(animation, "X"); break; } Storyboard.SetTarget(animation, tempTransform); var sb = new Storyboard(); sb.Duration = animation.Duration; sb.Children.Add(animation); var clippingParent = _image.Parent as FrameworkElement; RectangleGeometry clip = null; if (clippingParent != null) { clip = clippingParent.Clip; var transformToParent = _image.TransformToVisual(clippingParent); var topLeft = transformToParent.TransformPoint(new Point(0, 0)); topLeft = new Point(Math.Max(0, topLeft.X), Math.Max(0, topLeft.Y)); var bottomRight = transformToParent.TransformPoint(new Point(_image.ActualWidth, _image.ActualHeight)); bottomRight = new Point(Math.Min(clippingParent.ActualWidth, bottomRight.X), Math.Min(clippingParent.ActualHeight, bottomRight.Y)); clippingParent.Clip = new RectangleGeometry { Rect = new Rect( topLeft, bottomRight) }; } await sb.BeginAsync(); if (_image == null) { return; } if (clippingParent != null) { _image.Clip = clip; } _image.RenderTransform = oldTransform; }
/// <summary> /// Sets the ImageLoadedTransitionType property. This dependency property /// indicates the type of transition to use when the image loads. /// </summary> public static void SetImageLoadedTransitionType(DependencyObject d, ImageLoadedTransitionTypes value) { d.SetValue(ImageLoadedTransitionTypeProperty, value); }