0

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>

Mert Akcakaya
  • 3,109
  • 2
  • 31
  • 42

2 Answers2

0

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();
    }
JSJ
  • 5,653
  • 3
  • 25
  • 32
  • why dont you create instance of Enterance in code insteed of xaml. because you base.OnStartUp(e) runs before you logic. why you take control from application. you must take care of stairing. where to turn. try recheck the success after you done and put the show and shutdon logic. – JSJ Oct 19 '12 at 14:50
  • I don't think it is related to that. Try showing a window and close them. After that try showing another window and it simply won't show. I think it is related something special to Application class. – Mert Akcakaya Oct 20 '12 at 00:07
0

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

Mert Akcakaya
  • 3,109
  • 2
  • 31
  • 42