简体   繁体   中英

System.Windows.Application class instance won't show second window

I have a WPF desktop application where I want to check if I can connect to my database with EF before launching the main window. So I try to connect to database and if exception occurs, I simply show a window where I ask the user to enter correct database connection string.

After user enters the connection string, I retry connecting to database and if it connects I want to launch my main window and let the application start.

EDIT: I changed my code a liitle bit, but still doesn't work. I think it is about closing one window and then trying to show another window in Application class. It works if ServerConfigurationWin is not shown.

However what happens is, after I display the database configuration window to user, the application calls the StartupUri window's constructor declared in App.xaml file but nothing shows on screen.

Thanks.

Here is my App code:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        bool success = false;
        while (!success)
        {
            try
            {
                DemirbasContext context = new DemirbasContext();
                DbInitializer initializer = new DbInitializer();
                Database.SetInitializer<DemirbasContext>(initializer);
                context.Database.Initialize(false);
                success = true;
            }
            catch (ProviderIncompatibleException)
            {
                ServerConfigurationWin configurationWin = new ServerConfigurationWin();
                if (!configurationWin.ShowThemAll())
                {
                    // ShowThemAll() returns that operation is canceled.
                    App.Current.Shutdown();
                    return;
                }
                success = false;
            }
        }
    }

    public App()
    {

    }

    void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        // Process unhandled exception do stuff below
        Console.WriteLine("***Message***");
        Console.WriteLine(e.Exception.Message);
        Console.WriteLine("***Stack Trace***");
        Console.WriteLine(e.Exception.StackTrace);
        Console.WriteLine("***Target Site***");
        Console.WriteLine(e.Exception.TargetSite);
        // Prevent default unhandled exception processing
        e.Handled = true;
    }
}

App.xaml:

<Application x:Class="Demirbas.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         DispatcherUnhandledException="App_DispatcherUnhandledException"
         StartupUri="Enterance.xaml">
<Application.Resources>
    <Style TargetType="Button">
        <Setter Property="Height" Value="25" />
        <Setter Property="Width" Value="75" />
        <Setter Property="MinWidth" Value="25" />
        <Setter Property="Margin" Value="5,0" />
        <Setter Property="Padding" Value="0" />
    </Style>
    <Style TargetType="Label">
        <Setter Property="Margin" Value="5,0" />
    </Style>

</Application.Resources>

you shoud try this one when your WPF applicaitno starts it looking for entry point to invoke you main window. you can change the default main window by using App.Xaml's StartupUri. or you can override the startup invocation like this.

App.Xaml.cs Class

 protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        bool condition = false;

        //you logic. / you code.

        if (condition)
            (App.Current.MainWindow = new MyMainWindow ()).Show();
        else
            App.Current.Shutdown();
    }

It seems you cannot show a second window from your Application class. So I had to shutdown the application and start again.

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