WPF中實(shí)現(xiàn)彈出進(jìn)度條窗口的示例詳解
實(shí)現(xiàn)功能
模擬一個(gè)任務(wù)開(kāi)始執(zhí)行,在窗口彈出一個(gè)進(jìn)度條,展示執(zhí)行進(jìn)度,執(zhí)行完成彈出提示框。例如做數(shù)據(jù)查詢(xún)時(shí),如果查詢(xún)需要一段時(shí)間,操作人員可以很好的知道是否查詢(xún)完成。
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)度條窗口后臺(tái)代碼:
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)建測(cè)試主窗口
主窗口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">開(kāi)始任務(wù)...</Button>
</Grid>
</Window>主窗口后臺(tái)代碼:
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. 測(cè)試驗(yàn)證如下

到此這篇關(guān)于WPF中實(shí)現(xiàn)彈出進(jìn)度條窗口的示例詳解的文章就介紹到這了,更多相關(guān)WPF彈出進(jìn)度條窗口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用SQLDMO操作數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了C#使用SQLDMO操作數(shù)據(jù)庫(kù)的方法,實(shí)例分析了基于SQLDMO.dll動(dòng)態(tài)鏈接庫(kù)操作數(shù)據(jù)庫(kù)的相關(guān)技巧,需要的朋友可以參考下2015-06-06
在C# WPF下自定義滾動(dòng)條ScrollViewer樣式的操作
這篇文章主要介紹了在C# WPF下自定義滾動(dòng)條ScrollViewer樣式的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
C#/.Net 中快速批量給SQLite數(shù)據(jù)庫(kù)插入測(cè)試數(shù)據(jù)
這篇文章主要介紹了C#/.Net 中快速批量給SQLite數(shù)據(jù)庫(kù)插入測(cè)試數(shù)據(jù),本文直接給出實(shí)例代碼,需要的朋友可以參考下2015-06-06
Js中的substring,substr與C#中的Substring比較
本篇文章主要是對(duì)Js中的substring,substr與C#中的Substring進(jìn)行了比較。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01
C#實(shí)現(xiàn)實(shí)體類(lèi)和XML的相互轉(zhuǎn)換
本文詳細(xì)講解了C#實(shí)現(xiàn)實(shí)體類(lèi)和XML的相互轉(zhuǎn)換,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02

