关闭

[置顶] WPF 后台任务 等待动画 样例

标签: C#WPF动画后台任务异步
1923人阅读 评论(0) 收藏 举报
分类:

运行效果:



前台代码:

<Window x :Class="Waiting.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="后台忙" Height="94.635" Width="197.361">
    <Grid >
        <Button Content ="开始" HorizontalAlignment="Left" Margin="10,10,0,0" Name ="button1" VerticalAlignment="Top" Click="button1_Click" Width ="48" />
        <Label Name ="lab_pro" Content="" Height="25" VerticalAlignment ="Top" HorizontalAlignment="Left" Margin="80,20,0,0" />
        <!--动画代码,只要填写name属性即可-->
        <Grid Name ="loading" Visibility="Collapsed" Height="41" Grid.Row ="0" VerticalAlignment="Top" Margin="126,10,0,0" HorizontalAlignment="Left" Width ="42">
            <Grid.Resources>
                <DrawingBrush x :Key="brush" Stretch="None" AlignmentX ="Center" AlignmentY="Top">
                    <DrawingBrush.Drawing>
                        <GeometryDrawing Brush ="Black">
                            <GeometryDrawing.Geometry>
                                <EllipseGeometry RadiusX ="2" RadiusY="5"/>
                            </GeometryDrawing.Geometry>
                        </GeometryDrawing>
                    </DrawingBrush.Drawing>
                </DrawingBrush>
            </Grid.Resources>

            <Rectangle x :Name="r01" Fill="{StaticResource brush}" Opacity ="0.5" RenderTransformOrigin="0.5,0.5">
                <Rectangle.RenderTransform>
                    <RotateTransform Angle ="0"/>
                </Rectangle.RenderTransform>
            </Rectangle>
            <Rectangle x :Name="r02" Fill="{StaticResource brush}" Opacity ="0.5" RenderTransformOrigin="0.5,0.5">
                <Rectangle.RenderTransform>
                    <RotateTransform Angle ="30"/>
                </Rectangle.RenderTransform>
            </Rectangle>
            <Rectangle x :Name="r03" Fill="{StaticResource brush}" Opacity ="0.5" RenderTransformOrigin="0.5,0.5">
                <Rectangle.RenderTransform>
                    <RotateTransform Angle ="60"/>
                </Rectangle.RenderTransform>
            </Rectangle>
            <Rectangle x :Name="r04" Fill="{StaticResource brush}" Opacity ="0.5" RenderTransformOrigin="0.5,0.5">
                <Rectangle.RenderTransform>
                    <RotateTransform Angle ="90"/>
                </Rectangle.RenderTransform>
            </Rectangle>
            <Rectangle x :Name="r05" Fill="{StaticResource brush}" Opacity ="0.5" RenderTransformOrigin="0.5,0.5">
                <Rectangle.RenderTransform>
                    <RotateTransform Angle ="120"/>
                </Rectangle.RenderTransform>
            </Rectangle>
            <Rectangle x :Name="r06" Fill="{StaticResource brush}" Opacity ="0.5" RenderTransformOrigin="0.5,0.5">
                <Rectangle.RenderTransform>
                    <RotateTransform Angle ="150"/>
                </Rectangle.RenderTransform>
            </Rectangle>
            <Rectangle x :Name="r07" Fill="{StaticResource brush}" Opacity ="0.5" RenderTransformOrigin="0.5,0.5">
                <Rectangle.RenderTransform>
                    <RotateTransform Angle ="180"/>
                </Rectangle.RenderTransform>
            </Rectangle>
            <Rectangle x :Name="r08" Fill="{StaticResource brush}" Opacity ="0.5" RenderTransformOrigin="0.5,0.5">
                <Rectangle.RenderTransform>
                    <RotateTransform Angle ="210"/>
                </Rectangle.RenderTransform>
            </Rectangle>
            <Rectangle x :Name="r09" Fill="{StaticResource brush}" Opacity ="0.5" RenderTransformOrigin="0.5,0.5">
                <Rectangle.RenderTransform>
                    <RotateTransform Angle ="240"/>
                </Rectangle.RenderTransform>
            </Rectangle>
            <Rectangle x :Name="r10" Fill="{StaticResource brush}" Opacity ="0.5" RenderTransformOrigin="0.5,0.5">
                <Rectangle.RenderTransform>
                    <RotateTransform Angle ="270"/>
                </Rectangle.RenderTransform>
            </Rectangle>
            <Rectangle x :Name="r11" Fill="{StaticResource brush}" Opacity ="0.5" RenderTransformOrigin="0.5,0.5">
                <Rectangle.RenderTransform>
                    <RotateTransform Angle ="300"/>
                </Rectangle.RenderTransform>
            </Rectangle>
            <Rectangle x :Name="r12" Fill="{StaticResource brush}" Opacity ="0.5" RenderTransformOrigin="0.5,0.5">
                <Rectangle.RenderTransform>
                    <RotateTransform Angle ="330"/>
                </Rectangle.RenderTransform>
            </Rectangle>

            <Grid.Triggers>
                <EventTrigger RoutedEvent ="Grid.Loaded">
                    <BeginStoryboard>
                        <Storyboard RepeatBehavior ="Forever">
                            <DoubleAnimation Storyboard.TargetName ="r01" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.08333" BeginTime ="0:0:0.00000" To="0"/>
                            <DoubleAnimation Storyboard.TargetName ="r02" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.08333" BeginTime ="0:0:0.08333" To="0"/>
                            <DoubleAnimation Storyboard.TargetName ="r03" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.08333" BeginTime ="0:0:0.16666" To="0"/>
                            <DoubleAnimation Storyboard.TargetName ="r04" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.08333" BeginTime ="0:0:0.24999" To="0"/>
                            <DoubleAnimation Storyboard.TargetName ="r05" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.08333" BeginTime ="0:0:0.33332" To="0"/>
                            <DoubleAnimation Storyboard.TargetName ="r06" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.08333" BeginTime ="0:0:0.41665" To="0"/>
                            <DoubleAnimation Storyboard.TargetName ="r07" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.08333" BeginTime ="0:0:0.49998" To="0"/>
                            <DoubleAnimation Storyboard.TargetName ="r08" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.08333" BeginTime ="0:0:0.58331" To="0"/>
                            <DoubleAnimation Storyboard.TargetName ="r09" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.08333" BeginTime ="0:0:0.66664" To="0"/>
                            <DoubleAnimation Storyboard.TargetName ="r10" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.08333" BeginTime ="0:0:0.74997" To="0"/>
                            <DoubleAnimation Storyboard.TargetName ="r11" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.08333" BeginTime ="0:0:0.83330" To="0"/>
                            <DoubleAnimation Storyboard.TargetName ="r12" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.08333" BeginTime ="0:0:0.91663" To="0"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Grid.Triggers>
        </Grid>               
    </Grid >
</Window>
后台代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Waiting
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        BackgroundWorker bgMeet;

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            bgMeet = new BackgroundWorker ();
            //能否报告进度更新
            bgMeet.WorkerReportsProgress = true;
            //要执行的后台任务
            bgMeet.DoWork += new DoWorkEventHandler (bgMeet_DoWork);
            //进度报告方法
            bgMeet.ProgressChanged += new ProgressChangedEventHandler (bgMeet_ProgressChanged);
            //后台任务执行完成时调用的方法
            bgMeet.RunWorkerCompleted += new RunWorkerCompletedEventHandler (bgMeet_RunWorkerCompleted);
            bgMeet.RunWorkerAsync(); //任务启动
        }              
        //执行任务
        void bgMeet_DoWork(object sender, DoWorkEventArgs e)
        {
            //开始播放等待动画
            this.Dispatcher.Invoke(new Action(() =>
            {
                loading.Visibility = System.Windows. Visibility.Visible;
            }));
            //开始后台任务
            GetData();
        }
        //报告任务进度
        void bgMeet_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                this.lab_pro.Content = e.ProgressPercentage + "%";
            }));
        }
        //任务执行完成后更新状态
        void bgMeet_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            loading.Visibility = System.Windows. Visibility.Collapsed;
            this.Dispatcher.Invoke(new Action(() =>
            {
                this.lab_pro.Content = "完成" ;
            }));
        }
        //模拟耗时任务
        public void GetData()
        {
            for (int i = 0; i < 6; i++)
            {
                bgMeet.ReportProgress(20*i);
                System.Threading. Thread.Sleep(400);
            }
        }
    }
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:24125次
    • 积分:458
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:0篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论