internal static ConnectedAnimationAdorner FindFrom([NotNull] Visual visual) { if (Window.GetWindow(visual)?.Content is UIElement root) { var layer = AdornerLayer.GetAdornerLayer(root); if (layer != null) { var adorner = layer.GetAdorners(root)?.OfType <ConnectedAnimationAdorner>().FirstOrDefault(); if (adorner == null) { adorner = new ConnectedAnimationAdorner(root); layer.Add(adorner); } return(adorner); } } throw new InvalidOperationException("指定的 Visual 尚未连接到可见的视觉树中,找不到用于承载动画的容器。"); }
public bool TryStart([NotNull] UIElement destination, [NotNull] IEnumerable <UIElement> coordinatedElements) { if (destination == null) { throw new ArgumentNullException(nameof(destination)); } if (coordinatedElements == null) { throw new ArgumentNullException(nameof(coordinatedElements)); } if (Equals(_source, destination)) { return(false); } // 正在播动画?动画播完废弃了?false // 准备播放连接动画。 var adorner = ConnectedAnimationAdorner.FindFrom(destination); var connectionHost = new ConnectedVisual(_source, destination); adorner.Children.Add(connectionHost); var storyboard = new Storyboard(); var animation = new DoubleAnimation(0.0, 1.0, new Duration(TimeSpan.FromSeconds(10.6))) { EasingFunction = new CubicEase { EasingMode = EasingMode.EaseInOut }, }; Storyboard.SetTarget(animation, connectionHost); Storyboard.SetTargetProperty(animation, new PropertyPath(ConnectedVisual.ProgressProperty.Name)); storyboard.Children.Add(animation); storyboard.Completed += (sender, args) => { _reportCompleted(this, EventArgs.Empty); //destination.ClearValue(UIElement.VisibilityProperty); adorner.Children.Remove(connectionHost); }; //destination.Visibility = Visibility.Hidden; storyboard.Begin(); return(true); }