欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WPF中實(shí)現(xiàn)彈出進(jìn)度條窗口的示例詳解

 更新時(shí)間:2024年11月25日 10:16:27   作者:Ritchie.Lee  
這篇文章主要為大家詳細(xì)介紹了如何WPF中實(shí)現(xiàn)彈出進(jìn)度條窗口,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

實(shí)現(xiàn)功能

模擬一個任務(wù)開始執(zhí)行,在窗口彈出一個進(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#使用SQLDMO操作數(shù)據(jù)庫的方法

    C#使用SQLDMO操作數(shù)據(jù)庫的方法

    這篇文章主要介紹了C#使用SQLDMO操作數(shù)據(jù)庫的方法,實(shí)例分析了基于SQLDMO.dll動態(tài)鏈接庫操作數(shù)據(jù)庫的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • 淺談C# 中的委托和事件

    淺談C# 中的委托和事件

    本篇文章主要介紹C# 中的委托和事件,委托和事件在 .Net Framework中的應(yīng)用非常廣泛,有興趣的可以了解一下。
    2016-12-12
  • 在C# WPF下自定義滾動條ScrollViewer樣式的操作

    在C# WPF下自定義滾動條ScrollViewer樣式的操作

    這篇文章主要介紹了在C# WPF下自定義滾動條ScrollViewer樣式的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • C#/.Net 中快速批量給SQLite數(shù)據(jù)庫插入測試數(shù)據(jù)

    C#/.Net 中快速批量給SQLite數(shù)據(jù)庫插入測試數(shù)據(jù)

    這篇文章主要介紹了C#/.Net 中快速批量給SQLite數(shù)據(jù)庫插入測試數(shù)據(jù),本文直接給出實(shí)例代碼,需要的朋友可以參考下
    2015-06-06
  • Js中的substring,substr與C#中的Substring比較

    Js中的substring,substr與C#中的Substring比較

    本篇文章主要是對Js中的substring,substr與C#中的Substring進(jìn)行了比較。需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • C#網(wǎng)頁信息采集方法匯總

    C#網(wǎng)頁信息采集方法匯總

    這篇文章主要介紹了C#網(wǎng)頁信息采集方法,實(shí)例匯總了三種常用的方法,是非常實(shí)用的技巧,需要的朋友可以參考下
    2014-10-10
  • VS2019使用快捷鍵將代碼對齊的方法

    VS2019使用快捷鍵將代碼對齊的方法

    這篇文章主要介紹了VS2019使用快捷鍵將代碼對齊的相關(guān)資料,非常不錯對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • 用c# 自動更新程序

    用c# 自動更新程序

    這篇文章主要介紹了用c# 自動更新程序的代碼示例,幫助大家更好的理解和使用c#編程語言,感興趣的朋友可以了解下
    2020-11-11
  • C#實(shí)現(xiàn)實(shí)體類和XML的相互轉(zhuǎn)換

    C#實(shí)現(xiàn)實(shí)體類和XML的相互轉(zhuǎn)換

    本文詳細(xì)講解了C#實(shí)現(xiàn)實(shí)體類和XML的相互轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • C#實(shí)現(xiàn)梳排序的使用示例

    C#實(shí)現(xiàn)梳排序的使用示例

    梳排序算法是一種改進(jìn)的冒泡排序算法,它通過調(diào)整冒泡排序的間隔來提高排序的效率,本文主要介紹了C#實(shí)現(xiàn)梳排序的使用示例,感興趣的可以了解一下
    2023-11-11

最新評論