WPF ShowDialog 在第二次调用时立即返回 null
我认为这是 WPF 框架中的一个错误,没有深入了解我的程序以及为什么我要做我正在做的事情,我编写了一个简单的测试应用程序来证明我的理论。
这个问题能否得到证实? 在将应用程序放入其运行循环之前执行一系列对话框有哪些可能的解决方法?
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();
}
}
}
更新:
当然我可以改变我的设计来适应这种行为。 然而我想做的事情其实很简单。
我从 Application
派生了我的 MyApplication
。 在 Main()
上,我启动了一系列启动检查,例如许可证信息、启动屏幕、连接和配置检查等。 当我一切都清楚后,我调用 MyApplicatiom.Run(MyMainForm)
。
此设计基于正常的 Windows 应用程序,可以正常运行。
非常感谢您的帮助,我会记住您不能在 application.Run()
之前调用 ShowDialog()
,因为它只是初始化关闭。 然而,我本以为关闭序列应该只在 App.Run() 指令之后启动。
如果我理解错误,请纠正我。
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.
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)
.
This design is based on a normal Windows application which works without a problem.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不是错误。
Application
的默认ShutdownMode
是OnLastWindowClosed
,因此一旦第一个窗口关闭,您的应用程序就会开始关闭! 更改为OnExplicitShutdown
它将起作用,但您必须管理关闭。我认为您可能只需要重新思考您实际上想要实现的目标。 为什么在应用程序启动期间会显示两个连续的对话框窗口?
Not a bug. The default
ShutdownMode
ofApplication
isOnLastWindowClosed
, so as soon as the first window is closed your application will start shutting down! Change toOnExplicitShutdown
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?
对于遇到同样问题的任何人,以下是解决该问题的方法:
For anyone that has the same problem, here is how you can get around it: