Hope this makes sense.
I have something like this:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
if (!Directory.Exists(dataFolder))
{
Directory.CreateDirectory(dataFolder);
}
try
{
using (DataContext context = new DataContext())
{
context.Database.CreateIfNotExists();
}
}
catch (IOException ex)
{
}
KeyProgram.Show();
if (Manager.KeyExists == true)
{
MainWindowViewModel viewModel = new MainWindowViewModel();
this.MainWindow = new MainWindow();
this.MainWindow.DataContext = viewModel;
this.ShutdownMode = System.Windows.ShutdownMode.OnMainWindowClose;
Helper.WindowDialogService.SetOwner(this.MainWindow);
viewModel.Init();
this.MainWindow.Show();
}
else {
Console.WriteLine("Please try again");
}
}
After showing my licensekey window, I want to break so that the system wants for the user input (Have the user type in license key) then proceed to run the if-else statement (at if (LicenseKeyManager.licenseKeyExists == true)).
However currently, onStartup, the application just runs all of the code first then if I type in the key and validate it, it does not run that if statement because it has already ran.
How do I break for the user input from the view before continuing to that if-statement?
Right now after LicenseKeyProgram.Show(), if I set a breakpoint at the if-statement, the application won't let the user input anything because it's stuck on loading (Can't perform any action on the window).
Do I need an event handler here or...?
Use ShowDialog instead than Show.
Relevant MSDN link:
http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog%28v=vs.110%29.aspx
Oh wait, one more question, I can get the user input but the appplication does not ever run/hit the if-else statement? Why?
You have to setup the mainwindow before calling the showdialog on your other window or else it'll trigger the shutdown of the application (since the application will shutdown when all windows are closed).
Example:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow window = new MainWindow();
Window1 test = new Window1();
test.ShowDialog();
if (test.InvalidLicense)
{
Shutdown();
return;
}
window.Show();
}
Ressources:
WPF showing dialog before main window
http://www.ageektrapped.com/blog/the-wpf-application-class-overview-and-gotcha/