WPF 后台任务 等待动画 样例

运行效果:

前台代码:

[csharp] view plain copy

  1. <Window x :Class=“Waiting.Window1”
  2.         xmlns=“http://schemas.microsoft.com/winfx/2006/xaml/presentation”
  3.         xmlns:x =“http://schemas.microsoft.com/winfx/2006/xaml”
  4.         Title=“后台忙” Height=“94.635” Width=“197.361”>
  5.     <Grid >
  6.         <Button Content =“开始” HorizontalAlignment=“Left” Margin=“10,10,0,0” Name =“button1” VerticalAlignment=“Top” Click=“button1_Click” Width =“48” />
  7.         <Label Name =“lab_pro” Content=“” Height=“25” VerticalAlignment =“Top” HorizontalAlignment=“Left” Margin=“80,20,0,0” />
  8.         <!–动画代码,只要填写name属性即可–>
  9.         <Grid Name =“loading” Visibility=“Collapsed” Height=“41” Grid.Row =“0” VerticalAlignment=“Top” Margin=“126,10,0,0” HorizontalAlignment=“Left” Width =“42”>
  10.             <Grid.Resources>
  11.                 <DrawingBrush x :Key=“brush” Stretch=“None” AlignmentX =“Center” AlignmentY=“Top”>
  12.                     <DrawingBrush.Drawing>
  13.                         <GeometryDrawing Brush =“Black”>
  14.                             <GeometryDrawing.Geometry>
  15.                                 <EllipseGeometry RadiusX =“2” RadiusY=“5”/>
  16.                             </GeometryDrawing.Geometry>
  17.                         </GeometryDrawing>
  18.                     </DrawingBrush.Drawing>
  19.                 </DrawingBrush>
  20.             </Grid.Resources>
  21.             <Rectangle x :Name=“r01” Fill=“{StaticResource brush}” Opacity =“0.5” RenderTransformOrigin=“0.5,0.5”>
  22.                 <Rectangle.RenderTransform>
  23.                     <RotateTransform Angle =“0”/>
  24.                 </Rectangle.RenderTransform>
  25.             </Rectangle>
  26.             <Rectangle x :Name=“r02” Fill=“{StaticResource brush}” Opacity =“0.5” RenderTransformOrigin=“0.5,0.5”>
  27.                 <Rectangle.RenderTransform>
  28.                     <RotateTransform Angle =“30”/>
  29.                 </Rectangle.RenderTransform>
  30.             </Rectangle>
  31.             <Rectangle x :Name=“r03” Fill=“{StaticResource brush}” Opacity =“0.5” RenderTransformOrigin=“0.5,0.5”>
  32.                 <Rectangle.RenderTransform>
  33.                     <RotateTransform Angle =“60”/>
  34.                 </Rectangle.RenderTransform>
  35.             </Rectangle>
  36.             <Rectangle x :Name=“r04” Fill=“{StaticResource brush}” Opacity =“0.5” RenderTransformOrigin=“0.5,0.5”>
  37.                 <Rectangle.RenderTransform>
  38.                     <RotateTransform Angle =“90”/>
  39.                 </Rectangle.RenderTransform>
  40.             </Rectangle>
  41.             <Rectangle x :Name=“r05” Fill=“{StaticResource brush}” Opacity =“0.5” RenderTransformOrigin=“0.5,0.5”>
  42.                 <Rectangle.RenderTransform>
  43.                     <RotateTransform Angle =“120”/>
  44.                 </Rectangle.RenderTransform>
  45.             </Rectangle>
  46.             <Rectangle x :Name=“r06” Fill=“{StaticResource brush}” Opacity =“0.5” RenderTransformOrigin=“0.5,0.5”>
  47.                 <Rectangle.RenderTransform>
  48.                     <RotateTransform Angle =“150”/>
  49.                 </Rectangle.RenderTransform>
  50.             </Rectangle>
  51.             <Rectangle x :Name=“r07” Fill=“{StaticResource brush}” Opacity =“0.5” RenderTransformOrigin=“0.5,0.5”>
  52.                 <Rectangle.RenderTransform>
  53.                     <RotateTransform Angle =“180”/>
  54.                 </Rectangle.RenderTransform>
  55.             </Rectangle>
  56.             <Rectangle x :Name=“r08” Fill=“{StaticResource brush}” Opacity =“0.5” RenderTransformOrigin=“0.5,0.5”>
  57.                 <Rectangle.RenderTransform>
  58.                     <RotateTransform Angle =“210”/>
  59.                 </Rectangle.RenderTransform>
  60.             </Rectangle>
  61.             <Rectangle x :Name=“r09” Fill=“{StaticResource brush}” Opacity =“0.5” RenderTransformOrigin=“0.5,0.5”>
  62.                 <Rectangle.RenderTransform>
  63.                     <RotateTransform Angle =“240”/>
  64.                 </Rectangle.RenderTransform>
  65.             </Rectangle>
  66.             <Rectangle x :Name=“r10” Fill=“{StaticResource brush}” Opacity =“0.5” RenderTransformOrigin=“0.5,0.5”>
  67.                 <Rectangle.RenderTransform>
  68.                     <RotateTransform Angle =“270”/>
  69.                 </Rectangle.RenderTransform>
  70.             </Rectangle>
  71.             <Rectangle x :Name=“r11” Fill=“{StaticResource brush}” Opacity =“0.5” RenderTransformOrigin=“0.5,0.5”>
  72.                 <Rectangle.RenderTransform>
  73.                     <RotateTransform Angle =“300”/>
  74.                 </Rectangle.RenderTransform>
  75.             </Rectangle>
  76.             <Rectangle x :Name=“r12” Fill=“{StaticResource brush}” Opacity =“0.5” RenderTransformOrigin=“0.5,0.5”>
  77.                 <Rectangle.RenderTransform>
  78.                     <RotateTransform Angle =“330”/>
  79.                 </Rectangle.RenderTransform>
  80.             </Rectangle>
  81.             <Grid.Triggers>
  82.                 <EventTrigger RoutedEvent =“Grid.Loaded”>
  83.                     <BeginStoryboard>
  84.                         <Storyboard RepeatBehavior =“Forever”>
  85.                             <DoubleAnimation Storyboard.TargetName =“r01” Storyboard.TargetProperty=“Opacity” AutoReverse=“True” Duration=“0:0:0.08333” BeginTime =“0:0:0.00000” To=“0”/>
  86.                             <DoubleAnimation Storyboard.TargetName =“r02” Storyboard.TargetProperty=“Opacity” AutoReverse=“True” Duration=“0:0:0.08333” BeginTime =“0:0:0.08333” To=“0”/>
  87.                             <DoubleAnimation Storyboard.TargetName =“r03” Storyboard.TargetProperty=“Opacity” AutoReverse=“True” Duration=“0:0:0.08333” BeginTime =“0:0:0.16666” To=“0”/>
  88.                             <DoubleAnimation Storyboard.TargetName =“r04” Storyboard.TargetProperty=“Opacity” AutoReverse=“True” Duration=“0:0:0.08333” BeginTime =“0:0:0.24999” To=“0”/>
  89.                             <DoubleAnimation Storyboard.TargetName =“r05” Storyboard.TargetProperty=“Opacity” AutoReverse=“True” Duration=“0:0:0.08333” BeginTime =“0:0:0.33332” To=“0”/>
  90.                             <DoubleAnimation Storyboard.TargetName =“r06” Storyboard.TargetProperty=“Opacity” AutoReverse=“True” Duration=“0:0:0.08333” BeginTime =“0:0:0.41665” To=“0”/>
  91.                             <DoubleAnimation Storyboard.TargetName =“r07” Storyboard.TargetProperty=“Opacity” AutoReverse=“True” Duration=“0:0:0.08333” BeginTime =“0:0:0.49998” To=“0”/>
  92.                             <DoubleAnimation Storyboard.TargetName =“r08” Storyboard.TargetProperty=“Opacity” AutoReverse=“True” Duration=“0:0:0.08333” BeginTime =“0:0:0.58331” To=“0”/>
  93.                             <DoubleAnimation Storyboard.TargetName =“r09” Storyboard.TargetProperty=“Opacity” AutoReverse=“True” Duration=“0:0:0.08333” BeginTime =“0:0:0.66664” To=“0”/>
  94.                             <DoubleAnimation Storyboard.TargetName =“r10” Storyboard.TargetProperty=“Opacity” AutoReverse=“True” Duration=“0:0:0.08333” BeginTime =“0:0:0.74997” To=“0”/>
  95.                             <DoubleAnimation Storyboard.TargetName =“r11” Storyboard.TargetProperty=“Opacity” AutoReverse=“True” Duration=“0:0:0.08333” BeginTime =“0:0:0.83330” To=“0”/>
  96.                             <DoubleAnimation Storyboard.TargetName =“r12” Storyboard.TargetProperty=“Opacity” AutoReverse=“True” Duration=“0:0:0.08333” BeginTime =“0:0:0.91663” To=“0”/>
  97.                         </Storyboard>
  98.                     </BeginStoryboard>
  99.                 </EventTrigger>
  100.             </Grid.Triggers>
  101.         </Grid>
  102.     </Grid >
  103. </Window>

后台代码:

[csharp] view plain copy

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14. namespace Waiting
  15. {
  16.     /// <summary>
  17.     /// Window1.xaml 的交互逻辑
  18.     /// </summary>
  19.     public partial class Window1 : Window
  20.     {
  21.         public Window1()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.         BackgroundWorker bgMeet;
  26.         private void button1_Click(object sender, RoutedEventArgs e)
  27.         {
  28.             bgMeet = new BackgroundWorker ();
  29.             //能否报告进度更新
  30.             bgMeet.WorkerReportsProgress = true;
  31.             //要执行的后台任务
  32.             bgMeet.DoWork += new DoWorkEventHandler (bgMeet_DoWork);
  33.             //进度报告方法
  34.             bgMeet.ProgressChanged += new ProgressChangedEventHandler (bgMeet_ProgressChanged);
  35.             //后台任务执行完成时调用的方法
  36.             bgMeet.RunWorkerCompleted += new RunWorkerCompletedEventHandler (bgMeet_RunWorkerCompleted);
  37.             bgMeet.RunWorkerAsync(); //任务启动
  38.         }
  39.         //执行任务
  40.         void bgMeet_DoWork(object sender, DoWorkEventArgs e)
  41.         {
  42.             //开始播放等待动画
  43.             this.Dispatcher.Invoke(new Action(() =>
  44.             {
  45.                 loading.Visibility = System.Windows. Visibility.Visible;
  46.             }));
  47.             //开始后台任务
  48.             GetData();
  49.         }
  50.         //报告任务进度
  51.         void bgMeet_ProgressChanged(object sender, ProgressChangedEventArgs e)
  52.         {
  53.             this.Dispatcher.Invoke(new Action(() =>
  54.             {
  55.                 this.lab_pro.Content = e.ProgressPercentage + “%”;
  56.             }));
  57.         }
  58.         //任务执行完成后更新状态
  59.         void bgMeet_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  60.         {
  61.             loading.Visibility = System.Windows. Visibility.Collapsed;
  62.             this.Dispatcher.Invoke(new Action(() =>
  63.             {
  64.                 this.lab_pro.Content = “完成” ;
  65.             }));
  66.         }
  67.         //模拟耗时任务
  68.         public void GetData()
  69.         {
  70.             for (int i = 0; i < 6; i++)
  71.             {
  72.                 bgMeet.ReportProgress(20*i);
  73.                 System.Threading. Thread.Sleep(400);
  74.             }
  75.         }
  76.     }
  77. }

发表评论