一佳一

记录像1+1一样简洁的代码

导航

WPF防止界面卡死并显示加载中效果

Posted on 2013-02-06 10:04  一佳一  阅读(16560)  评论(3编辑  收藏  举报

网上貌似没有完整的WPF正在加载的例子,所以自己写了一个,希望能帮到有需要的同学

前台:

<Window x:Class="WpfApplication1.Loading"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Loading" Height="320" Width="570">
<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>

 

后台:

using System;
using System.Collections.Generic;
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;
using System.ComponentModel;

namespace WpfApplication1
{
/// <summary>
/// Loading.xaml 的交互逻辑
/// </summary>
public partial class Loading : Window
{
public Loading()
{
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);
}
}
}
}

 这个花了我不少时间总结的哦,要转载的同学 记得保存我的连接http://www.cnblogs.com/linyijia/archive/2013/02/06/2900609.html,不做纯粹的伸手党哦