WPF輕松實(shí)現(xiàn)進(jìn)度條的示例代碼
前言
本文講述如何在軟件啟動(dòng)和窗體按鈕操作時(shí)彈出進(jìn)度條。
運(yùn)行環(huán)境:Win10、VS2022
一、新建WPF項(xiàng)目
二、新建WPF窗體。
1、新建窗體,取名DefProcessBar.xaml。
2、設(shè)置窗體屬性、樣式。
<Window x:Class="WpfApp4.DefProcessBar" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp4" mc:Ignorable="d" Title="DefProcessBar" Width="300" Height="50" WindowStartupLocation="CenterScreen" WindowStyle="None" AllowsTransparency="True" Background="Transparent" ShowInTaskbar="False" ResizeMode="NoResize">
2.1、WindowStartupLocation="CenterScreen"是窗體在屏幕中間顯示;
2.2、WindowStyle="None"是不要窗體頂部?jī)?nèi)容;
2.3、AllowsTransparency="True"是允許窗體透明;
2.4、Background="Transparent"是設(shè)置透明背景;
2.5、ShowInTaskbar="False"是指示此窗口是否出現(xiàn)在任務(wù)欄;
2.6、ResizeMode="NoResize"是不可調(diào)節(jié)窗體大小。
3、添加進(jìn)度消息和進(jìn)度條。
<Grid.RowDefinitions> <RowDefinition Height="25"></RowDefinition> <RowDefinition Height="25"></RowDefinition> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Name="txtTitle" FontSize="16" Foreground="Yellow" HorizontalAlignment="Left" TextWrapping="Wrap" Margin="10, 0,0,0"></TextBlock> <ProgressBar Grid.Row="1" x:Name="progressBar" Maximum="100" Height="25" Width="300" Foreground="Green" Background="LightGray" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
4、添加 DefProcessBar.xaml.cs 代碼
/// <summary> /// DefProcessBar.xaml 的交互邏輯 /// </summary> public partial class DefProcessBar : Window { public DefProcessBar() { InitializeComponent(); } public void ShowProcess(int process, string title = "") { progressBar.Dispatcher.Invoke(() => { this.txtTitle.Text = title; this.progressBar.Value = process; }); } public void SetProcess(DefProcessBar defProcessBar, int process, string title = "") { if (process > 100) { process = 100; } else if (process < 0) { process = 0; } title += $" ({process}%)"; defProcessBar.ShowProcess(process, title); } }
三、在 App.xaml.cs 中使用
/// <summary> /// App.xaml 的交互邏輯 /// </summary> public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { Application.Current.ShutdownMode = System.Windows.ShutdownMode.OnExplicitShutdown; bool isFinish = false; //DialogWait defProcessBar = new DialogWait(); DefProcessBar defProcessBar = new DefProcessBar(); Task.Run(() => { defProcessBar.SetProcess(defProcessBar, 20, "啟動(dòng)中,請(qǐng)稍后..."); // 寫入實(shí)際處理代碼 Thread.Sleep(500); defProcessBar.SetProcess(defProcessBar, 40, "啟動(dòng)中,請(qǐng)稍后..."); Thread.Sleep(500); defProcessBar.SetProcess(defProcessBar, 60, "啟動(dòng)中,請(qǐng)稍后..."); Thread.Sleep(500); defProcessBar.SetProcess(defProcessBar, 80, "啟動(dòng)中,請(qǐng)稍后..."); Thread.Sleep(500); defProcessBar.Dispatcher.Invoke(() => { defProcessBar.Close(); }); }); defProcessBar.ShowDialog(); if (isFinish) { base.OnStartup(e); Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose; } else { this.Shutdown(); } } }
啟動(dòng)軟件查看效果。
四、在主窗體 MainWindow 中彈出進(jìn)度條。
1、添加按鈕。
2、添加按鈕事件處理。
private void Button_Click(object sender, RoutedEventArgs e) { DefProcessBar defProcessBar = new DefProcessBar(); Task.Run(() => { defProcessBar.SetProcess(defProcessBar, 20, "啟動(dòng)中,請(qǐng)稍后..."); Thread.Sleep(500); defProcessBar.SetProcess(defProcessBar, 40, "啟動(dòng)中,請(qǐng)稍后..."); Thread.Sleep(500); defProcessBar.SetProcess(defProcessBar, 60, "啟動(dòng)中,請(qǐng)稍后..."); Thread.Sleep(500); defProcessBar.SetProcess(defProcessBar, 80, "啟動(dòng)中,請(qǐng)稍后..."); //寫入實(shí)際處理代碼 Thread.Sleep(500); defProcessBar.Dispatcher.Invoke(() => { defProcessBar.Close(); }); }); defProcessBar.ShowDialog(); }
3、效果跟軟件啟動(dòng)時(shí)一樣。
缺點(diǎn):所有邏輯必須放到異步處理。
最后
以上就是WPF輕松實(shí)現(xiàn)進(jìn)度條的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于WPF實(shí)現(xiàn)進(jìn)度條的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C# paddlerocrsharp識(shí)別身份證號(hào)的實(shí)現(xiàn)示例
paddlerocrsharp可以進(jìn)行圖片識(shí)別,本文主要介紹了C# paddlerocrsharp識(shí)別身份證號(hào)的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02WPF實(shí)現(xiàn)Badge標(biāo)識(shí)的示例代碼
這篇文章主要為大家詳細(xì)介紹了WPF如何實(shí)現(xiàn)Badge標(biāo)識(shí),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下2023-06-06關(guān)于Unity中RectTransform與transform的區(qū)別
這篇文章主要介紹了Unity中RectTransform與transform的區(qū)別,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01C#通過(guò)Win32API設(shè)置客戶端系統(tǒng)時(shí)間的方法詳解
在日常工作中,有時(shí)可能會(huì)需要獲取或修改客戶端電腦的系統(tǒng)時(shí)間,比如軟件設(shè)置了Licence有效期,本文以一個(gè)簡(jiǎn)單的小例子,簡(jiǎn)述如何通過(guò)C#獲取和設(shè)置客戶端電腦的系統(tǒng)時(shí)間,僅供學(xué)習(xí)分享使用,如有不足之處,還請(qǐng)指正,需要的朋友可以參考下2024-06-06C#如何實(shí)現(xiàn)對(duì)sql server數(shù)據(jù)庫(kù)的增刪改查
本文的主要內(nèi)容是C#實(shí)現(xiàn)對(duì)sql server數(shù)據(jù)庫(kù)的增刪改查,示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-08-08Unity多語(yǔ)言轉(zhuǎn)換工具的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Unity多語(yǔ)言轉(zhuǎn)換工具的實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06微信公眾平臺(tái)開發(fā)教程(三) 基礎(chǔ)框架搭建
這篇文章主要介紹了微信公眾平臺(tái)開發(fā)教程(三) 基礎(chǔ)框架搭建,具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12