关闭

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

标签: C#WPF动画后台任务异步
2384人阅读 评论(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
查看评论

[WPF] 圆形等待效果

<!-- body, p, pre, span {font-family:Consolas,'Microsoft Yahei'; font-size:14px} p {line-height:150%} --> 自己做着玩儿的,留着以后用,...
  • qqamoon
  • qqamoon
  • 2011-11-22 19:57
  • 8354

WPF 让自己的缓冲动画永不卡死

让缓冲动画在一个新的线程中显示,不在主UI线程中处理。 个人总结:在STA线程模式中,对线程的所有调用都放到一个队列中,在WPF中的队列调度是DIspatcher来处理的。WPF在STA模型中可使用的最小单元是窗体,因为我们无法在一个窗体中展现两个不同Dispather的控件。
  • steel_1991
  • steel_1991
  • 2017-01-14 20:16
  • 1636

WPF 后台任务 等待动画 样例

运行效果: 前台代码: <Window x :Class="Waiting.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&qu...
  • zhumingyan
  • zhumingyan
  • 2015-12-14 14:57
  • 2384

WPF 实现加载等待状态

1.方法一:
  • wanlong360599336
  • wanlong360599336
  • 2014-09-18 09:53
  • 3900

[WPF] 圆形等待效果

转自:http://blog.csdn.net/qqamoon/article/details/7001693 自己做着玩儿的,留着以后用,效果类似下面的 GIF 动画。 [html] view plaincopy Grid...
  • dangercheng
  • dangercheng
  • 2013-11-28 11:58
  • 1943

WPF加载等待效果

  • 2017-01-13 11:03
  • 59KB
  • 下载

WPF程序启动加载splash screen等待的方法

最近做项目,点开WPF程序,界面半天不打开,要过一会界面才出来,于是想到给它做一个splash window. 最开始想到的方法是:程序启动的时候,new 一个窗口(带有动画的等待),调用Show方法不阻塞后面的进行来显示。如下所示:namespace WPF_SplashTest { /...
  • lishuangquan1987
  • lishuangquan1987
  • 2017-03-30 21:53
  • 1650

WPF 圆形Loading 等待画面

这个是对网上流传的一段代码进行稍微修改一下的代码
  • cjwix2012
  • cjwix2012
  • 2014-12-15 10:21
  • 2962

WPF 圆形等待loading效果

留着以后用,效果类似下面的 GIF 动画。
  • fighter_yy
  • fighter_yy
  • 2013-03-19 22:26
  • 1117

wpf 窗体等待动画

  • 2014-05-21 14:05
  • 206KB
  • 下载
    个人资料
    • 访问:29452次
    • 积分:505
    • 等级:
    • 排名:千里之外
    • 原创:23篇
    • 转载:0篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论