欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WPF ProgressBar实现实时进度效果

程序员文章站 2023-11-29 15:36:58
本文实例为大家分享了wpf progressbar实现实时进度的具体代码,供大家参考,具体内容如下 简单测试,页面如图: 利用上班的一点点空闲时间,做了个wpf...

本文实例为大家分享了wpf progressbar实现实时进度的具体代码,供大家参考,具体内容如下

WPF ProgressBar实现实时进度效果

简单测试,页面如图:

利用上班的一点点空闲时间,做了个wpf progressbar的例子,觉得有潜力可挖,故放上来供大家参考,有写的更好的请留言哦,方便交流学习,谢谢

这个xaml:

<page x:class="wpfbrowserappdrill.progressbarpage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  mc:ignorable="d" 
title="progressbarpage" loaded="page_loaded">


 <grid>
  <button margin="10" click="button_click" verticalalignment="top" height="23" width="100">进度条控制</button>
  <textblock name="txtjd" verticalalignment="top" horizontalalignment="center" margin="50">当前进度:</textblock>
  <progressbar height="20" horizontalalignment="center" margin="8,167,0,0" 
name="pb_import" verticalalignment="top" width="500" />
 </grid>
</page>

后台代码:

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.navigation;
using system.windows.shapes;
using system.threading;


namespace wpfbrowserappdrill
{
 /// <summary>
 /// progressbarpage.xaml 的交互逻辑
 /// </summary>
 public partial class progressbarpage : page
 {


  public progressbarpage()
  {
   initializecomponent(); 
  } 


  private void beginimport() 
  {
   double value = 0;
   double total = 100d;//得到循环次数
   while (value < total)
   {
    double jd = math.round(((value + 1) * (pb_import.maximum / total)), 4);


    pb_import.dispatcher.invoke(new action<system.windows.dependencyproperty, object>(pb_import.setvalue),
     system.windows.threading.dispatcherpriority.background,
     progressbar.valueproperty,
     jd);
    //这里是加数据或费时的操作,我这里让它挂起300毫秒
    thread.sleep(300);
    txtjd.text = "当前的进度是:" + (value + 1) + "(实际值)" + jd + "(百分比)";
    value++;
   }
  }
  private void button_click(object sender, routedeventargs e)
  {
   beginimport();
   ////new thread(new threadstart(beginimport)).start();


  }


  private void page_loaded(object sender, routedeventargs e)
  {  
   pb_import.maximum = 100;
   pb_import.value = 0; 


  } 


 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。