最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)功能

模擬一個(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)

    使用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的示例(附源碼)

    這篇文章主要介紹了c# WPF中通過雙擊編輯DataGrid中Cell的示例(附源碼),幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-03-03
  • C#使用NPOI讀取excel轉(zhuǎn)為DataSet

    C#使用NPOI讀取excel轉(zhuǎn)為DataSet

    這篇文章主要為大家詳細(xì)介紹了C#使用NPOI讀取excel轉(zhuǎn)為DataSet,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C#使用linq給List某個(gè)屬性值賦值方式

    C#使用linq給List某個(gè)屬性值賦值方式

    在C#中使用LINQ進(jìn)行數(shù)據(jù)查詢與修改的最佳實(shí)踐,推薦使用ForEach方法或傳統(tǒng)的foreach循環(huán)進(jìn)行List對象屬性賦值;避免直接在LINQ查詢表達(dá)式中執(zhí)行賦值操作;可考慮使用Select投影生成新列表以保持?jǐn)?shù)據(jù)不變性
    2026-06-06
  • C#實(shí)現(xiàn)鼠標(biāo)消息捕獲

    C#實(shí)現(xiàn)鼠標(biāo)消息捕獲

    這篇文章介紹了C#實(shí)現(xiàn)鼠標(biāo)消息捕獲的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • richtextbox控件插入鏈接代碼分享

    richtextbox控件插入鏈接代碼分享

    richtextbox控件插入鏈接,暫時(shí)使用這個(gè)來解決鏈接的中文文本顯示
    2013-12-12
  • c# GridControl的模糊查詢實(shí)現(xiàn)代碼

    c# GridControl的模糊查詢實(shí)現(xiàn)代碼

    這篇文章主要介紹了c# GridControl的模糊查詢實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2017-02-02
  • C#列出所有物理網(wǎng)絡(luò)適配器的方法

    C#列出所有物理網(wǎng)絡(luò)適配器的方法

    這篇文章主要介紹了C#列出所有物理網(wǎng)絡(luò)適配器的方法,實(shí)例分析了C#操作網(wǎng)絡(luò)設(shè)備的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • c#實(shí)現(xiàn)metro文件壓縮解壓示例

    c#實(shí)現(xiàn)metro文件壓縮解壓示例

    這篇文章主要介紹了c#實(shí)現(xiàn)metro文件壓縮解壓示例,實(shí)現(xiàn)了zip中增加一張新圖片、刪除文件的方法,需要的朋友可以參考下
    2014-03-03
  • C#連接Oracle數(shù)據(jù)庫的方法

    C#連接Oracle數(shù)據(jù)庫的方法

    這篇文章主要介紹了C#連接Oracle數(shù)據(jù)庫的方法,實(shí)例分析了C#連接Oracle數(shù)據(jù)庫的方法與主要實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-06-06

最新評論

梅州市| 阆中市| 新安县| 广宗县| 四会市| 日土县| 泌阳县| 清水河县| 喀喇| 蛟河市| 临武县| 夹江县| 沧州市| 深州市| 梅河口市| 华容县| 洪江市| 邵阳市| 日照市| 舞阳县| 宜春市| 雅江县| 灵川县| 竹溪县| 开化县| 新野县| 灌南县| 万荣县| 会东县| 辽中县| 凤翔县| 德保县| 视频| 宁强县| 巴南区| 通榆县| 山丹县| 邓州市| 元阳县| 广东省| 黎城县|