// ReSharper disable VirtualMemberNeverOverriden.Global protected virtual void OnStateChanged(ProgressState oldState, ProgressState newState) // ReSharper restore VirtualMemberNeverOverriden.Global { var peer = UIElementAutomationPeer.FromElement(this) as ProgressAutomationPeer; if (peer != null) { peer.InvalidatePeer(); } if (IsEnabled) { switch (newState) { case ProgressState.Busy: VisualStateManager.GoToState(this, "Busy", true); if (IndeterminateAnimation != null && IsIndeterminateAnimationRunning) { IsIndeterminateAnimationRunning = false; IndeterminateAnimation.Stop(this); } if (BusyAnimation != null) { BusyAnimation.Begin(this, Template, true); IsBusyAnimationRunning = true; } break; case ProgressState.Indeterminate: VisualStateManager.GoToState(this, "Indeterminate", true); if (BusyAnimation != null && IsBusyAnimationRunning) { IsBusyAnimationRunning = false; BusyAnimation.Stop(this); } if (IndeterminateAnimation != null) { IndeterminateAnimation.Begin(this, Template, true); IsIndeterminateAnimationRunning = true; } break; case ProgressState.Normal: VisualStateManager.GoToState(this, "Normal", true); if (IndeterminateAnimation != null && IsIndeterminateAnimationRunning) { IsIndeterminateAnimationRunning = false; IndeterminateAnimation.Stop(this); } if (BusyAnimation != null && IsBusyAnimationRunning) { IsBusyAnimationRunning = false; BusyAnimation.Stop(this); } break; } } }
/// <summary> /// 为某个数据加载器建立一个 BusyControl。 /// 用于在数据加载的时候,显示一个不停转动的 Busy 控件。 /// </summary> /// <param name="viewController"></param> /// <returns></returns> internal static FrameworkElement CreateBusyControl(ViewDataLoader viewController) { //生成busy控件 var busy = new BusyAnimation(); //绑定TextBox到对象属性 var bdIsRunning = new Binding("IsBusy"); bdIsRunning.Source = viewController.DataProvider; bdIsRunning.BindsDirectlyToSource = true; busy.SetBinding(BusyAnimation.IsRunningProperty, bdIsRunning); //绑定IsVisible var bdIsVisible = new Binding("IsBusy"); bdIsVisible.Source = viewController.DataProvider; bdIsVisible.BindsDirectlyToSource = true; bdIsVisible.Converter = new BooleanToVisibilityConverter(); busy.SetBinding(BusyAnimation.VisibilityProperty, bdIsVisible); return(busy); }
private void UpdateBusyAnimation() { if ((BusyAnimation == null || (BusyAnimation != null && BusyAnimation.Name == DefaultBusyAnimationName)) && Track != null && _busyBar != null) { if (BusyAnimation != null && IsBusyAnimationRunning) { IsBusyAnimationRunning = false; BusyAnimation.Stop(this); BusyAnimation.Remove(this); } // NOTE: Lack of contracts: Children always have collection instance Contract.Assume(_busyBar.Children != null); BusyAnimation = new Storyboard { Name = DefaultBusyAnimationName, RepeatBehavior = RepeatBehavior.Forever }; var firstCycleAnimations = new Collection <DoubleAnimation>(); var secondCycleAnimations = new Collection <DoubleAnimation>(); const double time = 0.25; const double durationTime = time * 2; const double beginTimeIncrement = time / 2; const double shortPauseTime = time; const double longPauseTime = time * 1.5; var partMotionTime = (_busyBar.Children.Count - 1) * beginTimeIncrement + durationTime + shortPauseTime; var length = Math.Min(Track.ActualWidth, Track.ActualHeight) * Math.PI; for (var i = 0; i < _busyBar.Children.Count; i++) { var element = (FrameworkElement)_busyBar.Children[_busyBar.Children.Count - i - 1]; if (element != null) { var elementLength = Math.Max(element.Width, element.Height); var index = (_busyBar.Children.Count - 1) / 2 - i; var endPosition = length / 2 + index * (elementLength * 2); var endAngle = endPosition / length * 360d; var duration = new Duration(TimeSpan.FromSeconds(durationTime)); var firstCycleAnimation = new DoubleAnimation(0d, endAngle, duration) { BeginTime = TimeSpan.FromSeconds(i * beginTimeIncrement) }; Storyboard.SetTarget(firstCycleAnimation, element); Storyboard.SetTargetProperty(firstCycleAnimation, new PropertyPath(AngleProperty)); var secondCycleAnimation = new DoubleAnimation(0d, endAngle, duration) { BeginTime = TimeSpan.FromSeconds(partMotionTime + durationTime + i * beginTimeIncrement) }; Storyboard.SetTarget(secondCycleAnimation, element); Storyboard.SetTargetProperty(secondCycleAnimation, new PropertyPath(AngleProperty)); firstCycleAnimations.Add(firstCycleAnimation); secondCycleAnimations.Add(secondCycleAnimation); } } for (var i = 0; i < _busyBar.Children.Count; i++) { var element = (FrameworkElement)_busyBar.Children[_busyBar.Children.Count - i - 1]; if (element != null) { var duration = new Duration(TimeSpan.FromSeconds(durationTime)); var firstCycleAnimation = new DoubleAnimation(360d, duration) { BeginTime = TimeSpan.FromSeconds(partMotionTime + i * beginTimeIncrement) }; Storyboard.SetTarget(firstCycleAnimation, element); Storyboard.SetTargetProperty(firstCycleAnimation, new PropertyPath(AngleProperty)); var secondCycleAnimation = new DoubleAnimation(360d, duration) { BeginTime = TimeSpan.FromSeconds(partMotionTime * 2 + durationTime + i * beginTimeIncrement) }; Storyboard.SetTarget(secondCycleAnimation, element); Storyboard.SetTargetProperty(secondCycleAnimation, new PropertyPath(AngleProperty)); var moveAnimation = new DoubleAnimation(-1.0, new Duration(TimeSpan.FromSeconds(0))) { BeginTime = TimeSpan.FromSeconds(partMotionTime * 2 + durationTime * 2 + i * beginTimeIncrement) }; Storyboard.SetTarget(moveAnimation, element); Storyboard.SetTargetProperty(moveAnimation, new PropertyPath(AngleProperty)); firstCycleAnimations.Add(firstCycleAnimation); secondCycleAnimations.Add(secondCycleAnimation); secondCycleAnimations.Add(moveAnimation); } } BusyAnimation.Duration = new Duration(TimeSpan.FromSeconds(longPauseTime + partMotionTime * 3 + shortPauseTime * 2 + durationTime)); // NOTE: Lack of contracts: Children always have collection instance Contract.Assume(BusyAnimation.Children != null); foreach (var animation in firstCycleAnimations) { BusyAnimation.Children.Add(animation); } foreach (var animation in secondCycleAnimations) { BusyAnimation.Children.Add(animation); } if (BusyAnimation.CanFreeze) { BusyAnimation.Freeze(); } if (State == ProgressState.Busy && IsEnabled) { BusyAnimation.Begin(this, Template, true); IsBusyAnimationRunning = true; } } }
private void UpdateBusyAnimation() { if ((BusyAnimation == null || (BusyAnimation != null && BusyAnimation.Name == DefaultBusyAnimationName)) && Track != null && _busyBar != null) { if (BusyAnimation != null && IsBusyAnimationRunning) { IsBusyAnimationRunning = false; BusyAnimation.Stop(this); BusyAnimation.Remove(this); } // NOTE: Lack of contracts: Children always have collection instance Contract.Assume(_busyBar.Children != null); BusyAnimation = new Storyboard { Name = DefaultBusyAnimationName, RepeatBehavior = RepeatBehavior.Forever }; const double time = 0.25; const double durationTime = time * 2; const double beginTimeIncrement = time / 2; const double shortPauseTime = time; const double longPauseTime = time * 1.5; var partMotionTime = (_busyBar.Children.Count - 1) * beginTimeIncrement + durationTime; var busyAnimations = new Collection <DoubleAnimation>(); var width = Track.ActualWidth; var height = Track.ActualHeight; for (var i = 0; i < _busyBar.Children.Count; i++) { var element = (FrameworkElement)_busyBar.Children[_busyBar.Children.Count - i - 1]; if (element != null) { var elementWidth = element.Width; var elementHeight = element.Height; var index = (_busyBar.Children.Count - 1) / 2 - i; var center = (Orientation == Orientation.Horizontal ? width : height) / 2; var margin = Orientation == Orientation.Horizontal ? elementWidth : elementHeight; var startPosition = -(Orientation == Orientation.Horizontal ? elementWidth : elementHeight) - 1; var endPosition = center + index * ((Orientation == Orientation.Horizontal ? elementWidth : elementHeight) + margin); var duration = new Duration(TimeSpan.FromSeconds(durationTime)); var animation = new DoubleAnimation(startPosition, endPosition, duration) { BeginTime = TimeSpan.FromSeconds(i * beginTimeIncrement) }; Storyboard.SetTarget(animation, element); Storyboard.SetTargetProperty(animation, new PropertyPath(Orientation == Orientation.Horizontal ? Canvas.LeftProperty : Canvas.TopProperty)); busyAnimations.Add(animation); } } for (var i = 0; i < _busyBar.Children.Count; i++) { var element = (FrameworkElement)_busyBar.Children[_busyBar.Children.Count - i - 1]; if (element != null) { var elementWidth = element.Width; var elementHeight = element.Height; var endPosition = (Orientation == Orientation.Horizontal ? width : height) + (Orientation == Orientation.Horizontal ? elementWidth : elementHeight) + 1; var duration = new Duration(TimeSpan.FromSeconds(durationTime)); var animation = new DoubleAnimation(endPosition, duration) { BeginTime = TimeSpan.FromSeconds(partMotionTime + shortPauseTime + i * beginTimeIncrement) }; Storyboard.SetTarget(animation, element); Storyboard.SetTargetProperty(animation, new PropertyPath(Orientation == Orientation.Horizontal ? Canvas.LeftProperty : Canvas.TopProperty)); busyAnimations.Add(animation); } } BusyAnimation.Duration = new Duration(TimeSpan.FromSeconds(partMotionTime * 2 + shortPauseTime + longPauseTime)); // NOTE: Lack of contracts: Children always have collection instance Contract.Assume(BusyAnimation.Children != null); foreach (var animation in busyAnimations) { BusyAnimation.Children.Add(animation); } if (BusyAnimation.CanFreeze) { BusyAnimation.Freeze(); } if (State == ProgressState.Busy && IsEnabled) { BusyAnimation.Begin(this, Template, true); IsBusyAnimationRunning = true; } } }
private void ViewLoaded(object sender, RoutedEventArgs e) { if (DataContext is IRolodexViewModel) { var model = DataContext as IRolodexViewModel; Grid root = null; if (Content is Grid) { root = Content as Grid; } else if (Content is Border) { if ((Content as Border).Child is Grid) { root = (Content as Border).Child as Grid; } } if (root != null) { if (root.Background == null) { root.Background = new SolidColorBrush(Colors.LightGray); } var curtainGrid = new Grid(); curtainGrid.SetValue(Canvas.ZIndexProperty, 9999); curtainGrid.Opacity = 0.6; curtainGrid.SetValue(Grid.RowSpanProperty, root.RowDefinitions.Count + 1); curtainGrid.SetValue(Grid.ColumnSpanProperty, root.ColumnDefinitions.Count + 1); var brush = new LinearGradientBrush { EndPoint = new Point(0.5, 1), StartPoint = new Point(0.5, 0) }; var stops = new GradientStopCollection(); var stop = new GradientStop { Color = new Color { R = 0x80, G = 0x74, B = 0xD4 } }; stops.Add(stop); stop = new GradientStop { Color = new Color { R = 0x80, G = 0x74, B = 0xD4 }, Offset = 1 }; stops.Add(stop); stop = new GradientStop { Color = new Color { R = 0xB7, G = 0x84, B = 0xD0 }, Offset = 0.5 }; stops.Add(stop); brush.GradientStops = stops; curtainGrid.Background = brush; var busyAnimation = new BusyAnimation { MinHeight = 48, MinWidth = 48, MaxHeight = 300, MaxWidth = 300, TabNavigation = System.Windows.Input.KeyboardNavigationMode.Cycle }; var binding = new Binding("IsBusy"); busyAnimation.HorizontalAlignment = HorizontalAlignment.Stretch; busyAnimation.VerticalAlignment = VerticalAlignment.Stretch; busyAnimation.SetBinding(BusyAnimation.IsRunningProperty, binding); curtainGrid.Children.Add(busyAnimation); binding = new Binding("IsBusy") { Converter = new BooleanToVisibilityConverter() }; curtainGrid.SetBinding(VisibilityProperty, binding); root.Children.Add(curtainGrid); if (model.IsBusy) { Dispatcher.BeginInvoke(() => busyAnimation.Focus()); } model.PropertyChanged += (o1, e1) => { if (e1.PropertyName == "IsBusy") { if (model.IsBusy) { busyAnimation.Focus(); } else { Focus(); } } }; Loaded -= ViewLoaded; } } }