wpf prism 在程序启动前加载进度条窗体 进度条从0到100 OnInitialized方法中使用异步的方式

关键操作,使方法OnInitialized 进行 异步
App
using FullApp2sfTreeDg.Modules.ModuleName;
using FullApp2sfTreeDg.Modules.ModuleName.ViewModels;
using FullApp2sfTreeDg.Services;
using FullApp2sfTreeDg.Services.Interfaces;
using FullApp2sfTreeDg.ViewModels;
using FullApp2sfTreeDg.Views;
using HandyControl.Tools.Extension;
using Prism.Ioc;
using Prism.Modularity;
using Syncfusion.SfSkinManager;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace FullApp2sfTreeDg
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {
        private Window _shell;
        private SplashScreenWindow _splashScreen;
        protected override      Window CreateShell()
        {
            // 1. 先显示启动画面
            _splashScreen = new SplashScreenWindow();
            _splashScreen.Show();

            // 2. 创建主Shell 
            _shell = Container.Resolve<MainWindow>();
            return _shell;
        } 
    
        private async Task UpdateProgressAsync(SplashScreenWindow window, int progress, string content)
        {
            await window.Dispatcher.InvokeAsync(() => window.UpdateProgress(progress, content));
        }
        protected override async void OnInitialized()
        {     
            await UpdateProgressAsync (_splashScreen, 10, "正在加载模块1...");
            await Task.Delay(500); // 模拟加载时间
            await UpdateProgressAsync (_splashScreen, 20, "正在加载模块2...");
            await Task.Delay(500); // 模拟加载时间
            await UpdateProgressAsync (_splashScreen, 30, "正在加载模块3...");
            await Task.Delay(500); // 模拟加载时间
            await UpdateProgressAsync (_splashScreen, 50, "正在加载模块4...");
            await Task.Delay(500); // 模拟加载时间
            await UpdateProgressAsync(_splashScreen, 80, "正在加载模块5...");
            await Task.Delay(500); // 模拟加载时间
            await UpdateProgressAsync(_splashScreen, 100, "正在加载模块6...");
            await Task.Delay(500); // 模拟加载时间
            // 4. 初始化完成后切换窗口
            _splashScreen.Close();
            //显示主窗口
            _shell.Show();
            //
            base.OnInitialized();
        }
 
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterSingleton<IMessageService, MessageService>();
            containerRegistry.RegisterDialog<MyProcess>();
          
          
    
        }

        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            moduleCatalog.AddModule<ModuleNameModule>();
        }
    }
}

 

MyProcess
<Window x:Class="FullApp2sfTreeDg.Views.MyProcess"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:FullApp2sfTreeDg.Views"
             mc:Ignorable="d" 
             Height="200" d:DesignWidth="800">
    <Grid>
        <StackPanel Background="LightGray">
            <ProgressBar Margin="5" Width="500" Height="30" x:Name="progressBar" Value="0"></ProgressBar>
            <ProgressBar Margin="5" Width="500"  Height="30"  x:Name="pp1" Value="{Binding Value,UpdateSourceTrigger=PropertyChanged}"></ProgressBar>
            <TextBlock  x:Name="progressText"  Margin="5" HorizontalAlignment="Center" FontSize="50"    Text="{Binding Value}"></TextBlock>
        </StackPanel>
   
    </Grid>
</Window>

  

MyProcess.cs
using FullApp2sfTreeDg.ViewModels;
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 FullApp2sfTreeDg.Views
{
    /// <summary>
    /// MyProcess.xaml 的交互逻辑
    /// </summary>
    public partial class MyProcess : Window
    {
        public MyProcess()
        {
            InitializeComponent();
            MyProcessViewModel myProcessViewModel = new MyProcessViewModel();
             this.DataContext= myProcessViewModel;
        }
        public void UpdateProgress(int progress)
        {
            progressBar.Value = progress;
            progressText.Text = $"{progress}%";
        }
    }
}

  

posted @ 2025-06-11 18:08  JohnnyLei  阅读(33)  评论(0)    收藏  举报