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

2021-06-06 05:02

阅读:654

标签:get   pac   class   sum   trigger   dia   hive   column   one   

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

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

前台:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Loading" Height="320" Width="570">

























 

后台:

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
{
///


/// 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;
}));
}

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 {
bgMeet.ReportProgress(i);
System.Threading.Thread.Sleep(500);
}
}
}
}

 这个花了我不少时间总结的哦,要转载的同学 记得保存我的连接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


评论


亲,登录后才可以留言!