最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

WPF通過線程使用ProcessBar的方法詳解

 更新時間:2019年04月11日 10:27:04   作者:風幻影  
這篇文章主要給大家介紹了關于WPF通過線程使用ProcessBar的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用WPF具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

前言

WPF下使用進度條也是非常方便的,如果直接采用循環(huán)然后給ProcessBar賦值,理論上是沒有問題的,不過這樣會卡主主UI線程,我們看到的效果等全部都結束循環(huán)后才出現(xiàn)最后的值。

所以需要采用線程或者后臺方式給進度條賦值的方式,以下通過線程來觸發(fā)事件觸發(fā)的方式來實現(xiàn)給進度條賦值。這樣就可以模擬我們在實際過程中處理數(shù)據(jù)的一種進度方式。

方法示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;

namespace WpfTestProcessBar
{
 /// <summary>
 /// MainWindow.xaml 的交互邏輯
 /// </summary>
 public partial class MainWindow : Window
 {
  public delegate void ProgressDelegate(int percent);
  public MainWindow()
  {
   InitializeComponent();
   ProgressEvent += MainWindow_ProgressEvent;
   beginImport();
  }
  void MainWindow_ProgressEvent(int percent)
  {
   Dispatcher.Invoke(new Action<System.Windows.DependencyProperty, object>(Pro.SetValue), System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, Convert.ToDouble(percent+ 1) });
   Dispatcher.Invoke(new Action<System.Windows.DependencyProperty, object>(label.SetValue), System.Windows.Threading.DispatcherPriority.Background, new object[] { Label.ContentProperty, Convert.ToString((percent + 1)+"%") }); 

  }
  private event ProgressDelegate ProgressEvent;
  private void beginImport()
  {
   Pro.Maximum = 100;
   Pro.Value = 0;
   label.Content = "0%";
   ThreadPool.QueueUserWorkItem(state =>
   {
    Thread.Sleep(2000);
    for (int i = 0; i < 100; i++)
    {
     if (ProgressEvent != null)
     {
      ProgressEvent(i);
     }
     Thread.Sleep(10);
    }
   });
  }
 }
}

以上只是一種實現(xiàn)方式,希望給有需要的人提供幫助。

效果如下:

總結

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。

相關文章

最新評論

安岳县| 南京市| 奈曼旗| 裕民县| 松滋市| 白朗县| 全州县| 太和县| 壶关县| 伽师县| 东城区| 平罗县| 新乡市| 西平县| 台安县| 射洪县| 阳春市| 富裕县| 马龙县| 法库县| 保靖县| 五台县| 永川市| 吉木萨尔县| 漳州市| 英吉沙县| 靖州| 长垣县| 囊谦县| 云霄县| 那坡县| 木兰县| 汝州市| 尚义县| 石楼县| 泗阳县| 无为县| 诸城市| 沈丘县| 峡江县| 治县。|