WPF中實(shí)現(xiàn)彈出進(jìn)度條窗口的示例詳解
實(shí)現(xiàn)功能
模擬一個(gè)任務(wù)開始執(zhí)行,在窗口彈出一個(gè)進(jìn)度條,展示執(zhí)行進(jìn)度,執(zhí)行完成彈出提示框。例如做數(shù)據(jù)查詢時(shí),如果查詢需要一段時(shí)間,操作人員可以很好的知道是否查詢完成。
1. 設(shè)計(jì)進(jìn)度條彈出窗口
進(jìn)度條窗口樣式設(shè)計(jì) XAML
<Window x:Class="WpfApp.ProgressBarWindow"
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:WpfApp"
mc:Ignorable="d"
WindowStartupLocation="CenterScreen"
WindowStyle="None"
ResizeMode="NoResize"
AllowsTransparency="True"
Topmost="True"
Title="ProgressBarWindow" Height="100" Width="800">
<Grid>
<ProgressBar Margin="5" x:Name="progressBar1"
HorizontalAlignment="Stretch"
Height="90"
VerticalAlignment="Center"
DataContext="{Binding}"
Value="{Binding Progress}"
Foreground="LightGreen"
>
</ProgressBar>
</Grid>
</Window>進(jìn)度條窗口后臺代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Shapes;
namespace WpfApp
{
/// <summary>
/// ProgressBarWindow.xaml 的交互邏輯
/// </summary>
public partial class ProgressBarWindow : Window
{
public ProgressBarWindow()
{
InitializeComponent();
DataContext = new ProgressViewModel(this);
}
}
}2.創(chuàng)建進(jìn)度條視圖模型ProgressViewModel
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace WpfApp
{
public class ProgressViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private Window myWindow;
public ProgressViewModel(Window wnd)
{
myWindow = wnd;
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private int _progress;
public int Progress
{
get { return _progress; }
set
{
_progress = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Progress)));
if (_progress == 100)
{
// 關(guān)閉進(jìn)度窗口
Application.Current.Dispatcher.Invoke(() =>
{
myWindow.Close();
});
}
}
}
}
}3. 創(chuàng)建測試主窗口
主窗口XAML設(shè)計(jì):
<Window x:Class="WpfApp.MainWindow"
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:WpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="250" Width="400">
<Grid>
<Button x:Name="btnTest" FontSize="18" Click="btnTest_Click" Margin="10 30 10 30">開始任務(wù)...</Button>
</Grid>
</Window>主窗口后臺代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 WpfApp
{
/// <summary>
/// MainWindow.xaml 的交互邏輯
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private static ProgressBarWindow pgbWindow;
private async void btnTest_Click(object sender, RoutedEventArgs e)
{
// 創(chuàng)建進(jìn)度窗口
pgbWindow = new ProgressBarWindow();
pgbWindow.Show();
// 模擬耗時(shí)任務(wù)
await Task.Run(() =>
{
for (int i = 0; i <= 100; i++)
{
pgbWindow.Dispatcher.Invoke(() =>
{
pgbWindow.progressBar1.Value= i;
Console.WriteLine(i);
});
System.Threading.Thread.Sleep(20);
}
});
MessageBox.Show("任務(wù)完成!");
}
}
}4. 測試驗(yàn)證如下

到此這篇關(guān)于WPF中實(shí)現(xiàn)彈出進(jìn)度條窗口的示例詳解的文章就介紹到這了,更多相關(guān)WPF彈出進(jìn)度條窗口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用C#代碼在Word文檔中設(shè)置段落縮進(jìn)
本文介紹了如何通過編程方式在Word文檔中設(shè)置段落縮進(jìn),主要左縮進(jìn)、右縮進(jìn)、首行縮進(jìn)和懸掛縮進(jìn)等內(nèi)容,詳細(xì)解釋了設(shè)置段落縮進(jìn)的具體步驟和代碼實(shí)現(xiàn),提供了示例代碼幫助讀者更好地理解和實(shí)現(xiàn),需要的朋友可以參考下2026-04-04
c# WPF中通過雙擊編輯DataGrid中Cell的示例(附源碼)
這篇文章主要介紹了c# WPF中通過雙擊編輯DataGrid中Cell的示例(附源碼),幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03
C#使用NPOI讀取excel轉(zhuǎn)為DataSet
這篇文章主要為大家詳細(xì)介紹了C#使用NPOI讀取excel轉(zhuǎn)為DataSet,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
c# GridControl的模糊查詢實(shí)現(xiàn)代碼
這篇文章主要介紹了c# GridControl的模糊查詢實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-02-02

