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

 

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

  async void 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;
      
        protected override      Window CreateShell()
        { 
            return Container.Resolve<MainWindow>();
        
        } 
    
      
        protected override async void OnInitialized()
        {  
            SplashScreenWindow _splashScreen = new SplashScreenWindow();
            _splashScreen.Show();
            // 3. 异步初始化
            IProgress<(int, string)> progressHandler = new Progress<(int,string)>(percent =>
            {
                _splashScreen.UpdateProgress(percent.Item1, percent.Item2);
            });

            //await InitializeApplicationAsync(progressHandler);
            // 模拟初始化步骤
            progressHandler .Report((10,"模块1"));
         
            await Task.Delay(100); // 模拟耗时操作

            progressHandler.Report((30, "模块2"));

            await Task.Delay(100); // 模拟耗时操作
            progressHandler.Report((50, "模块3"));
            await Task.Delay(100); // 模拟耗时操作

            progressHandler.Report((80, "模块4"));
            await Task.Delay(100); // 模拟耗时操作

            progressHandler.Report((100, "模块5"));
            await Task.Delay(100); // 模拟耗时操作
            // 4. 初始化完成后切换窗口
            _splashScreen.Close(); 

            base.OnInitialized();
        }
    
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterSingleton<IMessageService, MessageService>();
            containerRegistry.RegisterDialog<MyProcess>();
            ViewAViewModel.CrackSyncfusion();
            SfSkinManager.ApplyStylesOnApplication = true;
    
        }

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

  

MyProcess.xaml
<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:22  JohnnyLei  阅读(27)  评论(0)    收藏  举报