原文地址:http://www.cnblogs.com/linyijia/archive/2013/02/06/2900609.html
<Window x:Class="LoadingTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:LoadingTest" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="41,20,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> <DataGrid AutoGenerateColumns="False" Height="99" HorizontalAlignment="Left" Margin="40,70,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="441" /> <Canvas Name="loading" RenderTransformOrigin="0.5,0.5" Visibility="Collapsed" HorizontalAlignment="Center" VerticalAlignment="Center" Width="120" Height="120" Margin="187,76,241,85"> <Ellipse Width="15" Height="15" Canvas.Left="55" Canvas.Top="31" Stretch="Fill" Fill="#FFD1D1F7" Opacity="1.0"/> <Ellipse Width="15" Height="15" Canvas.Left="38" Canvas.Top="39" Stretch="Fill" Fill="Blue" Opacity="0.8"/> <Ellipse Width="15" Height="15" Canvas.Left="36" Canvas.Top="58" Stretch="Fill" Fill="#FF0000FE" Opacity="0.7"/> <Ellipse Width="15" Height="15" Canvas.Left="52" Canvas.Top="67" Stretch="Fill" Fill="Blue" Opacity="0.6"/> <Ellipse Width="15" Height="15" Canvas.Left="68" Canvas.Top="61" Stretch="Fill" Fill="#FF2E2EFF" Opacity="0.5"/> <Ellipse Width="15" Height="15" Canvas.Left="69" Canvas.Top="42" Stretch="Fill" Fill="#FF6F6FFF" Opacity="0.4"/> <Canvas.RenderTransform> <RotateTransform x:Name="SpinnerRotate" Angle="0" /> </Canvas.RenderTransform> <Canvas.Triggers> <EventTrigger RoutedEvent="ContentControl.Loaded"> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="SpinnerRotate" Storyboard.TargetProperty="(RotateTransform.Angle)" From="0" To="360" Duration="0:0:0.8" RepeatBehavior="Forever" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Canvas.Triggers> </Canvas> <Label Name="lab_pro" Content="Label" Height="28" Margin="140,14,315,239" /> </Grid> </Window>
public partial class MainWindow : Window { public MainWindow() { 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_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { loading.Visibility = System.Windows.Visibility.Collapsed; this.Dispatcher.Invoke(new Action(() => { this.lab_pro.Content = "完成"; })); } void bgMeet_ProgressChanged(object sender, ProgressChangedEventArgs e) { this.Dispatcher.Invoke(new Action(() => { this.lab_pro.Content = e.ProgressPercentage; })); } void bgMeet_DoWork(object sender, DoWorkEventArgs e) { this.Dispatcher.Invoke(new Action(() => { loading.Visibility = System.Windows.Visibility.Visible; })); GetData(); } public void GetData() { for (int i = 0; i < 11; i++) { bgMeet.ReportProgress(i); System.Threading.Thread.Sleep(500); } } }