您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關利用WPF怎么實現一個進度條功能,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
實現說明
1、下載MUI相關代碼或者dll文件;
2、工程中引入該dll,并引入其資源文件;
<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、在需要顯示進度條的頁面,加入控件(其實還是WPF控件,只是MUI擴展了其樣式而已);
<Label Margin="280,169,0,0" Style="{StaticResource BackGroundContentText}" x:Name="lblMainState" HorizontalAlignment="Left" VerticalAlignment="Top">正在啟動:</Label> <ProgressBar Margin="280,200,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="500" Minimum="0" x:Name="ProgressControlRealValue" Maximum="1" Value="0.1" Height="16" IsIndeterminate="False"/> <Label Margin="280,212,0,0" Style="{StaticResource BackGroundContentText}" x:Name="lblProcess" HorizontalAlignment="Left" VerticalAlignment="Top">正在加載地圖數據...</Label> <ProgressBar Margin="280,250,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Minimum="0" x:Name="ProgressControl" Width="500" Maximum="2" Height="16" IsIndeterminate="True" />
4、后臺實現,由于要根據情況更新進度文字及進度條的值。所以,這里用到了異步BackgroundWorker(具體可以網上查查相關資料);
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; namespace Monitor.Class { /// <summary> /// 異步操作 /// </summary> public class CWorker { /// <summary> /// 對象 /// </summary> private BackgroundWorker backgroundWorker; /// <summary> /// 后臺執行的操作 /// </summary> public Action BackgroundWork { get; set; } /// <summary> /// 后臺任務執行完畢后事件 /// </summary> public event EventHandler<BackgroundWorkerEventArgs> BackgroundWorkerCompleted; private BackgroundWorkerEventArgs _eventArgs;//異常參數 /// <summary> /// 構造 /// </summary> public CWorker() { _eventArgs = new BackgroundWorkerEventArgs(); backgroundWorker = new BackgroundWorker(); backgroundWorker.WorkerReportsProgress = true; backgroundWorker.WorkerSupportsCancellation = true; backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); } /// <summary> /// 開始工作 /// </summary> public void BegionWork() { if (backgroundWorker.IsBusy) return; backgroundWorker.RunWorkerAsync(); } /// <summary> /// 工作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { if (BackgroundWork != null) { try { BackgroundWork(); } catch (Exception ex) { _eventArgs.BackGroundException = ex; } } } /// <summary> /// 完成 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (this.BackgroundWorkerCompleted != null) { this.BackgroundWorkerCompleted(null, _eventArgs); } } } /// <summary> /// 事件 /// </summary> public class BackgroundWorkerEventArgs : EventArgs { /// <summary> /// 后臺程序運行時拋出的異常 /// </summary> public Exception BackGroundException { get; set; } } }
namespace Monitor { /// <summary> /// Splash.xaml 的交互邏輯 /// </summary> public partial class Splash : Window { MainWindow m_MainWindow = null;//主窗口 CWorker m_Work = null;//任務 public Splash() { InitializeComponent(); m_MainWindow = new MainWindow();//創建主窗口對象 m_Work = new CWorker(); m_Work.BackgroundWork = this.ProcessDo; m_Work.BackgroundWorkerCompleted += new EventHandler<BackgroundWorkerEventArgs>(m_Work_BackgroundWorkerCompleted); } /// <summary> /// 進度提示 /// </summary> public void ProcessDo() { m_MainWindow.InitData(this); } /// <summary> /// 移動 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this.DragMove(); } /// <summary> /// 窗口加載 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Window_Loaded(object sender, RoutedEventArgs e) { m_Work.BegionWork(); } /// <summary> /// 執行完成 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void m_Work_BackgroundWorkerCompleted(object sender, BackgroundWorkerEventArgs e) { m_MainWindow.Show(); this.Close(); } /// <summary> /// 賦值 /// </summary> /// <param name="text"></param> 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; } } }
上述就是小編為大家分享的利用WPF怎么實現一個進度條功能了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。