简体   繁体   中英

ShowDialog not showing WPF

Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;

var dialog = new Login();

dialog.ShowDialog();

var mainWindow = new MainWindow(dialog.success, DBInteraction.getPID(dialog.txtLoginUser.Text));

mainWindow.ShowDialog();

this.MainWindow = mainWindow;

if (mainWindow.ShowDialog() == true)
{

}

Strange thing is that the window never actually gets shown if i debug this it just jumps over those showDialog points and does not show them at all. My login is shown perfectly fine.

As you can see i already tried various recommend in other threads regarding this topic this.MainWindow = mainWindow and setting the ShutdownMode to explicit.

Full call:

/// <summary>
/// Interaktionslogik für "App.xaml"
/// </summary>
public partial class App : Application
{
    private void ApplicationStart(object sender, StartupEventArgs e)
    {

        Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
        var dialog = new Login();

        dialog.ShowDialog();

        var mainWindow = new MainWindow(dialog.success, DBInteraction.getPID(dialog.txtLoginUser.Text));

        mainWindow.ShowDialog();

        this.MainWindow = mainWindow;

        if (mainWindow.ShowDialog() == true)
        {

        }
    }
}

App.xaml

<Application x:Class="Boosting.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Startup="ApplicationStart"
         ShutdownMode="OnExplicitShutdown">
<Application.Resources>

</Application.Resources>
</Application>

This is what you need to do in case you didn't do it already :

  1. Remove the StartupUri from the Application XAML file

  2. Set startup handler in app XAML :

  3. Add the Show() to MainWindow after the dialog show :

     private void ApplicationStartup(object sender, StartupEventArgs e) { Current.ShutdownMode = ShutdownMode.OnExplicitShutdown; var dialog = new Login(); dialog.ShowDialog(); var mainWindow = new MainWindow(); mainWindow.ShowDialog(); } 
  4. One last step. WPF set the first created window as the MainWindow in an app. Show inside your Login.xaml.cs set the MainWindow to null so that the next windows created takes precedence.

      if (App.Current.MainWindow == this) { App.Current.MainWindow = null; } 

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