【WPF】MainWindow前添加加载窗口,发现加载过程中有白色无内容窗口闪烁一下的解决方案

问题描述:在MainWindow展示前添加了一个加载界面,用来处理一些耗时或需要提前加载的内容,但是发现加载窗口在加载的过程中会有一个白色没内容的窗口闪烁一下,其实是MainWindow的创建,即使给Visibility = Visibility.Hidden也是无用的主要问题用代码列出来
csharp 复制代码
 protected override Window CreateShell()
 {
     // 创建主窗口实例
     _mainWindow = Container.Resolve<MainWindow>();
     _mainWindow.Visibility = Visibility.Hidden;
     return _mainWindow;
 }
        
 protected override void InitializeShell(Window shell)
 {
     // 不调用 base.InitializeShell(shell),避免自动显示主窗口
     // 改为显示加载窗口,并启动加载流程
     _loadingWindow = new LoadingWindow();
     _loadingWindow.Show();

     // 异步加载配置
     _ = LoadConfigurationAsync();
 }
 
private async Task LoadConfigurationAsync()
{
    try
    {
        // 启动进度动画,持续 1 秒
        await _loadingWindow.StartLoadingAsync(1);

        if (Global.Instance.WebView2Environment == null)
        {
            Global.Instance.WebView2Environment = await CoreWebView2Environment.CreateAsync();
        }

        // 加载完成,关闭加载窗口并显示主窗口
        await Dispatcher.InvokeAsync(() =>
        {
            _loadingWindow.Close();
            _mainWindow.Visibility = Visibility.Visible;
            _mainWindow.Show();
            Application.Current.MainWindow = _mainWindow;
        });
    }
    catch (Exception ex)
    {
        LogException(ex, "配置加载异常");
        _loadingWindow.Close();
        MessageBox.Show("配置加载失败,程序将退出。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
        this.Shutdown();
    }
}

修改后

csharp 复制代码
// 重写 Prism 方法,因为我们手动控制窗口
protected override Window CreateShell() => null;
protected override void InitializeShell(Window shell) { }
protected override void OnInitialized() => base.OnInitialized();

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    // 1. ★ 创建主窗口并隐藏,作为应用程序主窗口 ★
    _mainWindow = Container.Resolve<MainWindow>();
    _mainWindow.Visibility = Visibility.Hidden;
    Application.Current.MainWindow = _mainWindow;
    // 确保 ShutdownMode 为 OnMainWindowClose(默认就是)
    Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;

    // 2. 显示加载窗口
    _loadingWindow = new LoadingWindow();
    _loadingWindow.Show();

    // 3. 异步加载配置
    _ = LoadConfigurationAsync();
}

private async Task LoadConfigurationAsync()
{
    try
    {
        // 启动进度动画(持续1秒)
        await _loadingWindow.StartLoadingAsync(1);

        // 预创建 WebView2 环境
        if (Global.Instance.WebView2Environment == null)
        {
            Global.Instance.WebView2Environment = await CoreWebView2Environment.CreateAsync();
        }

        // 加载完成,关闭加载窗口并显示主窗口
        await Dispatcher.InvokeAsync(() =>
        {
            _loadingWindow.Close();
            _mainWindow.Visibility = Visibility.Visible;
            _mainWindow.Show();
        });
    }
    catch (Exception ex)
    {
        LogException(ex, "配置加载异常");
        _loadingWindow.Close();
        MessageBox.Show("配置加载失败,程序将退出。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
        this.Shutdown();
    }
}

完结 主要就是不能交给CreateShell和InitializeShell去处理。

相关推荐
Vae_Mars1 天前
WPF中的ControlTemplate(控件模板)
wpf
SamChan901 天前
Python+ReportLab自动生成PDF翻译质量审计报告:从数据到可视化的完整方案
开发语言·python·ai·pdf·wpf
circuitsosk1 天前
多智能体协同在能源数据分析中的实践:分配-执行-校验闭环架构设计
python·数据分析·wpf·能源·并行计算·多智能体系统·agent协作
weixin199701080162 天前
《大促护航:电商API限流与降级,双11峰值500万调用的架构复盘》(附Python源码)
python·架构·wpf
hh9503 天前
gent Plan x DeepSeek Harness:多 Agent 协作场景下的中央编排实践
人工智能·wpf·adg·火山引擎·agent plan·adg成都社区
Vae_Mars3 天前
WPF中的Lazy<T>使用方法
wpf
zQ.ii3 天前
WPF 触发器学习笔记
笔记·学习·wpf
SamChan903 天前
用PostgreSQL+pgvector构建PDF翻译记忆库:向量相似度检索+增量更新实战
数据库·python·ai·postgresql·pdf·wpf
李高钢4 天前
开源控件库 HandyControl 介绍与简单实践:让 WPF 界面告别“上个世纪“
开源·wpf