Simulate vs to implement a nice progress bar for WPF and a vswpf progress bar

Source: Internet
Author: User

Simulate vs to implement a nice progress bar for WPF and a vswpf progress bar

In order to make the interface friendly, a progress bar prompt must be added for a long period of operation. The progress bar provided by WPF is not very nice and has no visual effect. Later, when installing VS2012, I found that the progress bar had a good effect during the installation process, so I checked the information online. I learned about ModernUI (open-source) at https://github.com/firstfloorsoftware/mui.

Later, I tried to write a Demo, which worked well. In addition, the tif file is specially recorded to help you see the effect. If you don't talk much about it, first show the effect:

I. effect display

A. VS2012 installation interface;

B. Try Demo:

II. Implementation

1. Download MUI-related code or dll files;

2. Introduce the dll in the project and introduce its resource files;

Copy codeThe Code is as follows:
<Application. Resources>
<ResourceDictionary>
<ResourceDictionary. MergedDictionaries>
<ResourceDictionary Source = "/FirstFloor. ModernUI; component/Assets/ModernUI. xaml"/>
<ResourceDictionary Source = "/FirstFloor. ModernUI; component/Assets/ModernUI. Light. xaml"/>
</ResourceDictionary. MergedDictionaries>
</ResourceDictionary>
</Application. Resources>

3. On the page where the progress bar needs to be displayed, add the control (in fact, it is also a WPF control, but MUI extends its style );

Copy codeThe Code is as follows:
<Label Margin = "280,169," Style = "{StaticResource BackGroundContentText}" x: Name = "lblMainState" HorizontalAlignment = "Left" verticalignment = "Top"> starting: </Label>
<ProgressBar Margin = "280,200, 500" HorizontalAlignment = "Left" verticalignment = "Top" Width = "" Minimum = "0" x: name = "ProgressControlRealValue" Maximum = "1" Value = "0.1" Height = "16" IsIndeterminate = "False"/>
<Label Margin = "280,212," Style = "{StaticResource BackGroundContentText}" x: name = "lblProcess" HorizontalAlignment = "Left" VerticalAlignment = "Top"> Loading map data... </Label>
<ProgressBar Margin = "280,250," HorizontalAlignment = "Left" verticalignment = "Top" Minimum = "0" x: name = "ProgressControl" Width = "500" Maximum = "2" Height = "16" IsIndeterminate = "True"/>

4. Background implementation, because the progress text and progress bar value should be updated according to the situation. Therefore, asynchronous BackgroundWorker is used here (you can check relevant information on the Internet );

Using System; using System. collections. generic; using System. linq; using System. text; using System. componentModel; namespace Monitor. class {// <summary> /// Asynchronous Operation /// </summary> public class CWorker {/// <summary> // object /// </summary> private BackgroundWorker backgroundWorker; /// <summary> /// operations performed in the background /// </summary> public Action BackgroundWork {get; set ;} /// <summary> /// event after the background task is executed /// </summar Y> public event EventHandler <BackgroundWorkerEventArgs> BackgroundWorkerCompleted; private BackgroundWorkerEventArgs _ eventArgs; // exception parameter /// <summary> // construct /// </summary> public CWorker () {_ eventArgs = new BackgroundWorkerEventArgs (); backgroundWorker = new BackgroundWorker (); backgroundWorker. workerReportsProgress = true; backgroundWorker. workersuppscanscancellation = true; backgroundWorker. doWor K + = new DoWorkEventHandler (backgroundworker=dowork); backgroundWorker. runWorkerCompleted + = new RunWorkerCompletedEventHandler (backgroundworkerappsrunworkercompleted);} // <summary> // start work /// </summary> public void BegionWork () {if (backgroundWorker. isBusy) return; backgroundWorker. runWorkerAsync ();} /// <summary> /// work /// </summary> /// <param name = "sender"> </param> /// <param name =" e "> </Param> private void backgroundworker=dowork (object sender, DoWorkEventArgs e) {if (BackgroundWork! = Null) {try {BackgroundWork ();} catch (Exception ex) {_ eventArgs. backGroundException = ex ;}}} /// <summary> /// complete /// </summary> /// <param name = "sender"> </param> /// <param name =" e "> </param> private void backgroundworkerappsrunworkercompleted (object sender, runWorkerCompletedEventArgs e) {if (this. backgroundWorkerCompleted! = Null) {this. backgroundWorkerCompleted (null, _ eventArgs) ;}}/// <summary> // event /// </summary> public class BackgroundWorkerEventArgs: eventArgs {// <summary> // Exception thrown when the background program is running /// </summary> public Exception BackGroundException {get; set ;}}}
Namespace Monitor {// <summary> // Splash. interaction logic of xaml // </summary> public partial class Splash: Window {MainWindow m_MainWindow = null; // Main Window CWorker m_Work = null; // task public Splash () {InitializeComponent (); m_MainWindow = new MainWindow (); // create the main window object m_Work = new CWorker (); m_Work.BackgroundWork = this. processDo; m_Work.BackgroundWorkerCompleted + = new EventHandler <BackgroundWorkerEventArgs> (m _ Work_BackgroundWorkerCompleted);} // <summary> // progress prompt // </summary> public void ProcessDo () {m_main1_1_initdata (this );} /// <summary> /// move /// </summary> /// <param name = "sender"> </param> /// <param name =" e "> </param> private void Grid_MouseLeftButtonDown (object sender, mouseButtonEventArgs e) {this. dragMove () ;}/// <summary> /// window loading // </summary> /// <param name = "sender"> </param> /// <Param name = "e"> </param> private void Window_Loaded (object sender, RoutedEventArgs e) {m_Work.BegionWork ();} /// <summary> /// execution completed /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> void m_Work_BackgroundWorkerCompleted (object sender, backgroundWorkerEventArgs e) {m_mainmediaworkflow show (); this. close () ;}/// <summary> /// value assignment /// </summary> /// <param name = "text"> </pa Ram> private delegate void SetProcessLabelDelegate (string text, double processValue); public void SetProcessValue (string text, double processValue) {if (! Dispatcher. checkAccess () {Dispatcher. invoke (DispatcherPriority. send, new SetProcessLabelDelegate (SetProcessValue), text, processValue); return;} this. lblProcess. content = text; this. progressControlRealValue. value = processValue ;}}}

The above is all the content of this article. I hope you will like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.