1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > WPF-动态加载

WPF-动态加载

时间:2024-03-29 19:41:33

相关推荐

WPF-动态加载

添加一个UserControl,命名为BusyIndicator,view为空,在其.cs文件中定义一个类

1 /// <summary>2 /// 动态实体3 /// </summary>4 public class AnimationObject5 {6 public DoubleAnimation DoubleAnimation { get; set; }7 8 public TranslateTransform TranslateTransform { get; set; }9 }

定义属性

1 #region Properties 2 3 /// <summary> 4 /// 系统是否后台运行 5 /// </summary> 6 public bool IsBusy 7 { 8 get 9 {10 return (bool)GetValue(IsBusyProperty);11 }12 set13 {14 SetValue(IsBusyProperty, value);15 IsWorking = value;16 if (value)17 {18 BeginAnimation();19 //显示20 base.Visibility = System.Windows.Visibility.Visible;21 }22 else23 {24 //隐藏25 base.Visibility = System.Windows.Visibility.Collapsed;26 }27 }28 }29 30 /// <summary>31 /// 是否在运行32 /// </summary>33 private bool IsWorking { get; set; }34 35 /// <summary>36 /// 圆点的宽37 /// </summary>38 public new double? Width { get; set; }39 40 /// <summary>41 /// 圆点的高42 /// </summary>43 public new double? Height { get; set; }44 45 /// <summary>46 /// 圆点移动的间隔时间,秒47 /// </summary>48 public double? WaitTimeSpan { get; set; }49 50 /// <summary>51 /// 圆点的数量52 /// </summary>53 public int? EllipseCount { get; set; }54 55 /// <summary>56 /// 57 /// </summary>58 public List<AnimationObject> Ellipses { get; set; }59 60 /// <summary>61 /// 填充画刷62 /// </summary>63 public Brush Fill { get; set; }64 65 /// <summary>66 /// 轮廓画刷67 /// </summary>68 public Brush Stroke { get; set; }69 70 /// <summary>71 /// 圆点的间隔72 /// </summary>73 public int? Space { get; set; }74 75 /// <summary>76 /// 圆点的播放时长77 /// </summary>78 public double? Duration { get; set; }79 80 #endregion

View Code 添加事件

1 #region Methods 2 3 4 5 /// <summary> 6 /// 加载完毕事件 7 /// </summary> 8 /// <param name="sender"></param> 9 /// <param name="e"></param> 10 private void WinViewLoaded(object sender, RoutedEventArgs e) 11 { 12 InitializateData(); 13 } 14 15 /// <summary> 16 /// 初始化数据 17 /// </summary> 18 private void InitializateData() 19 { 20 if (!Width.HasValue) 21 { 22 Width = 10; 23 } 24 if (!Height.HasValue) 25 { 26 Height = 10; 27 } 28 if (!EllipseCount.HasValue) 29 { 30 EllipseCount = 10; 31 } 32 if (!WaitTimeSpan.HasValue) 33 { 34 WaitTimeSpan = 0.3; 35 } 36 if (!Space.HasValue) 37 { 38 Space = 10; 39 } 40 if (!this.Duration.HasValue) 41 { 42 this.Duration = 0.5; 43 } 44 45 Ellipses = new List<AnimationObject>(); 46 TopGrid.Children.Clear(); 47 for (int i = 0; i < EllipseCount; i++) 48 { 49 Ellipses.Add(SetEllipse(i)); 50 } 51 } 52 53 /// <summary> 54 /// 设置圆点 55 /// </summary> 56 /// <param name="_index"></param> 57 /// <returns></returns> 58 private AnimationObject SetEllipse(int _index) 59 { 60 //绘制一个椭圆 61 Ellipse e = new Ellipse(); 62 63 e.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; 64 e.VerticalAlignment = System.Windows.VerticalAlignment.Bottom; 65 e.Width = Width.Value; 66 e.Height = Height.Value; 67 e.Fill = Fill; 68 e.Stroke = Stroke; 69 e.StrokeThickness = 2; 70 TransformGroup tg = new TransformGroup(); 71 e.RenderTransform = tg; 72 TranslateTransform tt = new TranslateTransform(); 73 tg.Children.Add(tt); 74 DoubleAnimation da = new DoubleAnimation(); 75 da.Name = "DA" + _index.ToString(); 76 da.From = 0; 77 da.To = TopGrid.ActualWidth / 2 + (EllipseCount * Space) / 2 - _index * Space; 78 da.Duration = new Duration(TimeSpan.FromSeconds(this.Duration.Value)); 79 da.BeginTime = TimeSpan.FromSeconds(_index * WaitTimeSpan.Value); 80 pleted += (s, x) => 81 { 82 DoubleAnimation thisDa = (s as AnimationClock).Timeline as DoubleAnimation; 83 int DaIndex = int.Parse(thisDa.Name.Replace("DA", "")); 84 DoubleAnimation da2 = new DoubleAnimation(); 85 da2.From = thisDa.To; 86 da2.BeginTime = thisDa.BeginTime.Value.Add(TimeSpan.FromSeconds((EllipseCount.Value - 1) * WaitTimeSpan.Value - DaIndex * WaitTimeSpan.Value)); 87 da2.To = TopGrid.ActualWidth + Space * EllipseCount - Space - DaIndex * Space; 88 da2.Duration = new Duration(TimeSpan.FromSeconds(this.Duration.Value)); 89 if (_index == EllipseCount - 1) //最后一个点二阶段的动画都已结束 90 { 91 pleted += (ss, ee) => 92 { 93if (IsBusy) BeginAnimation(); 94else IsWorking = false; 95 }; 96 } 97 tt.BeginAnimation(TranslateTransform.XProperty, da2); 98 }; 99 TopGrid.Children.Add(e);100 101 return new AnimationObject() { DoubleAnimation = da,TranslateTransform = tt };102 }103 104 /// <summary>105 /// 开始变动106 /// </summary>107 private void BeginAnimation()108 {109 IsWorking = true;110 foreach (var ell in Ellipses)111 {112 ell.TranslateTransform.BeginAnimation(TranslateTransform.XProperty, ell.DoubleAnimation);113 }114 }115 116 #endregion

View Code 最后运行

1 /// <summary> 2 /// 3 /// </summary> 4 public BusyIndicator() 5 { 6 InitializeComponent(); 7 8 Fill = new SolidColorBrush(Colors.DarkBlue); 9 Stroke = new SolidColorBrush(Colors.White);10 InitializateData();11 this.Loaded += WinViewLoaded;12 }13 14 15 public static DependencyProperty IsBusyProperty =16 DependencyProperty.RegisterAttached("IsBusy",17 typeof(bool), 18 typeof(BusyIndicator),19 new FrameworkPropertyMetadata((bool)false, IsBusyChanged));20 21 /// <summary>22 /// 是否后台运行的变化事件23 /// </summary>24 /// <param name="sender"></param>25 /// <param name="e"></param>26 private static void IsBusyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)27 {28 BusyIndicator waiting = sender as BusyIndicator;29 if (waiting == null)30 {31 return;32 }33 else34 {35 waiting.IsBusy = (bool)e.NewValue;36 }37 }

View Code

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。