zoukankan      html  css  js  c++  java
  • WPF-动态加载

    1. 添加一个UserControl,命名为BusyIndicator,view为空,在其.cs文件中定义一个类
      1         /// <summary>
      2         /// 动态实体
      3         /// </summary>
      4        public class AnimationObject
      5        {
      6            public DoubleAnimation DoubleAnimation { get; set; }
      7 
      8            public TranslateTransform TranslateTransform { get; set; }
      9        }
    2. 定义属性
      1.  1    #region Properties
         2 
         3         /// <summary>
         4         /// 系统是否后台运行
         5         /// </summary>
         6         public bool IsBusy
         7         {
         8             get
         9             {
        10                 return (bool)GetValue(IsBusyProperty);
        11             }
        12             set
        13             {
        14                 SetValue(IsBusyProperty, value);
        15                 IsWorking = value;
        16                 if (value)
        17                 {
        18                     BeginAnimation();
        19                     //显示
        20                     base.Visibility = System.Windows.Visibility.Visible;
        21                 }
        22                 else
        23                 {
        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
    3. 添加事件
      1.   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             da.Completed += (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                     da2.Completed += (ss, ee) =>
         92                     {
         93                         if (IsBusy) BeginAnimation();
         94                         else 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
    4. 最后运行
    5.  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             else
      34             {
      35                 waiting.IsBusy = (bool)e.NewValue;
      36             }
      37         }
      View Code
  • 相关阅读:
    xshell常用命令
    linux传输文件命令: rz 和 sz
    使用openssh-clients的scp命令来传输文件
    RabbitMQ初学之二:直接发送消息到队列
    [App]华为P6设置与Xamarin Studio连通测试
    [Boost]图形处理库Boost::Polygon
    [App]Taste VS2015 && Android Studio
    [Linux&Vim]输入输出流
    [SSD大法好]神舟K480-I5-D3鸟枪换炮M6S
    [IoLanguage]Io Tutorial[转]
  • 原文地址:https://www.cnblogs.com/shadow-fei/p/4625521.html
Copyright © 2011-2022 走看看