简体   繁体   中英

Weird behaviour of Window.ShowDialog

I just got a weird behavior in my WPF application, hope somebody will give me a clue. I have the following overriden Application's OnStartup:

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

        try
        {
            if (AppMutex.WaitOne(TimeSpan.Zero, true))
            {
                IoC.Instance.RegisterInstance(LogManager.GetLogger("MainLogger"));
                Init();

                var mainView = new MainView()
                {
                    DataContext = IoC.Instance.MainViewModel                                            
                };

                mainView.ShowDialog();                  
            }
        }
        catch (Exception exception)
        {
            IoC.Instance.Log.Error(exception);
            throw;
        }
        finally
        {
            AppMutex.ReleaseMutex();
        }

    }

Here MainView is a subclass of System.Windows.Window and has no overriden methods. The trouble is with mainView.ShowDialog(). This line is executing without any exceptions but the window isn't shown. Thread just goes forward and application terminates. Has somebody any ideas about this behavior? Thanks in advance!

UPD: If I place MessageBox.ShowDialog() before this call, then MessageBox.ShowDialog executing correct, but mainView.ShowDialog throws an exception:

Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed.

at System.Windows.Window.VerifyCanShow()
at System.Windows.Window.ShowDialog() 

with null InnerException.

UPD: I got symbols and checked VerifyCanShow:

private void VerifyCanShow()
{
  if (this._disposed)
    throw new InvalidOperationException(SR.Get("ReshowNotAllowed"));
}

Looks like my mainView getting disposed in some way...

You need to set Application.ShutdownMode to OnExplicitShutdown . http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode(v=vs.110).aspx

You can then manually shut down the application after your dialog closes.

mainView.ShowDialog();
Shutdown();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM