WPF ShowDialog returns null immediately on second call

旧巷老猫 提交于 2019-12-03 23:21:23
Kent Boogaart

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?

For anyone that has the same problem, here is how you can get around it:

public App()
{
    // Preserve and temporarily switch shutdown mode
    var oldShutdownMode = ShutdownMode;
    ShutdownMode = ShutdownMode.OnExplicitShutdown;

    var dialog = new Window();
    var result = dialog.ShowDialog();
    dialog = new Window();
    result = dialog.ShowDialog(); // This will show!

    // Reset shutdown mode to original value
    ShutdownMode = oldShutdownMode;    
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!