WPF ShowDialog 在第二次调用时立即返回 null [英] WPF ShowDialog returns null immediately on second call

查看:79
本文介绍了WPF ShowDialog 在第二次调用时立即返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是 WPF 框架中的一个错误,没有深入了解我的程序以及我为什么要做我正在做的事情,我编写了一个简单的测试应用程序来证明我的理论.

I think this is a bug in the WPF framework, without going into depths of my program and why I am doing what I am doing, I wrote a simple test application to prove my theory.

可以确认这个问题吗?在将应用程序放入其运行循环之前要执行的一系列对话框有哪些可能的解决方法?

Can this issue be confirmed? What are possible workarounds for a series of dialogs to be executed before putting the application into its run loop?

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

namespace ShowDialogWindow
{
  /// <summary>
  /// Interaction logic for App.xaml
  /// </summary>
  public partial class App : Application
  {
    protected override void OnStartup(StartupEventArgs e)
    {
       Window testWindow = new Window();
       testWindow.ShowDialog();
       testWindow.Close();
       // THE CODE BELOW WILL NOT SHOW THE NEXT WINDOW
       Window testWindow2 = new Window();
       testWindow2.ShowDialog();
       testWindow2.Close();
    }
  }
}

更新:

当然,我可以更改我的设计以适应这种行为.然而,我想要做的很简单.

Update:

Sure I can change my design to accommodate this behaviour. What I was trying to do was really simple however.

我从 Application 派生了我的 MyApplication.在 Main() 上,我启动了一系列启动检查,例如许可证信息、启动画面、连接和配置检查等.当我一切都清楚后,我调用 MyApplicatiom.Run(MyMainForm).

I have derived my MyApplication from Application. On the Main(), I initiate a series of start checks, for example, license information, splash screen, connectivity and configuration checks, ect. When I get my all clear, I call MyApplicatiom.Run(MyMainForm).

此设计基于正常运行的普通 Windows 应用程序.

This design is based on a normal Windows application which works without a problem.

非常感谢您的帮助,我会记住您不能在 application.Run() 之前调用 ShowDialog() 因为它只是初始化关闭.然而,我本以为只有在 App.Run() 指令之后才应该启动关闭序列.

Thanks a lot for your help, and I will remember that you cannot call ShowDialog() before application.Run() as it simply innitializes a shutdown. I would have thought however that a shutdown sequence should only be initiated after a App.Run() instruction.

如果我理解错误,请纠正我.

Please correct me if I am understanding this wrong.

推荐答案

不是错误.Application 的默认 ShutdownModeOnLastWindowClosed,所以只要第一个窗口关闭,您的应用程序就会开始关闭!更改为 OnExplicitShutdown 即可使用,但您必须管理关闭.

Not a bug. The default ShutdownMode of Application is OnLastWindowClosed, so as soon as the first window is closed your application will start shutting down! Change to OnExplicitShutdown and it will work, but you'll have to manage the shutdown.

我认为您可能只需要重新考虑您实际想要实现的目标.为什么在应用启动期间会显示两个后续的对话窗口?

I think you probably just need to rethink what you're actually trying to achieve. Why would you display two subsequent dialog windows during the startup of your app?

这篇关于WPF ShowDialog 在第二次调用时立即返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆