wpf 在程序启动前加载进度条窗体 进度条从0到100

.关键操作,使方法OnStartup进行 异步

App

using System.Configuration;
using System.Data;
using System.Windows;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override async void  OnStartup (StartupEventArgs e)
        {
            base.OnStartup(e);
            var splashScreen = new SplashScreenWindow();
            splashScreen.Show();

            // 模拟异步加载过程

            await UpdateProgressAsync(splashScreen, 10,"加载数据库");
            await Task.Delay(500);

            await UpdateProgressAsync(splashScreen, 30, "加载ini");
            await Task.Delay(500);

            await UpdateProgressAsync(splashScreen, 50, "加载json");
            await Task.Delay(500);

            await UpdateProgressAsync(splashScreen, 90, "加载xml");
            await Task.Delay(500);

            await UpdateProgressAsync(splashScreen, 100, "加载config");
            await Task.Delay(500);

            MainWindow mainWindow = new MainWindow();
            splashScreen.Close();
            mainWindow.Show();
        }

        private async Task UpdateProgressAsync(SplashScreenWindow window, int progress,string content)
        {
            await window.Dispatcher.InvokeAsync(() => window.UpdateProgress(progress, content));
        }
    }

}

  SplashScreenWindow

<Window x:Class="WpfApp1.SplashScreenWindow"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="SplashScreenWindow" Height="450" Width="800">
    <Grid>
        <StackPanel Orientation="Vertical">
            <ProgressBar Name="progressBar"  Margin="10"  Height="20"  Value="0" Maximum="100"/>
            <TextBlock Name="progressText"  Margin="10"  HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBlock Name="tb_content" Margin="10" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </StackPanel>
    </Grid>
</Window>

  SplashScreenWindow

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;
using System.Windows.Threading;

namespace WpfApp1
{
    /// <summary>
    /// SplashScreenWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SplashScreenWindow : Window
    {
        public SplashScreenWindow()
        {
            InitializeComponent();
   
        } 
        public void UpdateProgress(int progress,string content)
        {
         
            progressBar.Value = progress;
            progressText.Text = $"{progress}%";
            tb_content.Text = content;
        }
    }
}

  

posted @ 2025-06-11 14:17  JohnnyLei  阅读(71)  评论(0)    收藏  举报