WPF防止界面卡死并显示加载中效果
2021-06-06 05:02
标签:get pac class sum trigger dia hive column one 网上貌似没有完整的WPF正在加载的例子,所以自己写了一个,希望能帮到有需要的同学 前台: 后台: using System; namespace WpfApplication1 void bgMeet_DoWork(object sender, DoWorkEventArgs e) 这个花了我不少时间总结的哦,要转载的同学 记得保存我的连接http://www.cnblogs.com/linyijia/archive/2013/02/06/2900609.html,不做纯粹的伸手党哦 WPF防止界面卡死并显示加载中效果 标签:get pac class sum trigger dia hive column one 原文地址:https://www.cnblogs.com/lonelyxmas/p/10789523.html
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Loading" Height="320" Width="570">
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;
{
///
/// Loading.xaml 的交互逻辑
///
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;
}));
}
{
this.Dispatcher.Invoke(new Action(() =>
{
loading.Visibility = System.Windows.Visibility.Visible;
}));
GetData();
}
public void GetData()
{
for (int i = 0; i {
bgMeet.ReportProgress(i);
System.Threading.Thread.Sleep(500);
}
}
}
}