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>