tags:

views:

87

answers:

2

I have this small piece of code.

private void Application_Startup(object sender, StartupEventArgs e)
        {
            WndAbout ab = new WndAbout();
            ab.Show();
        }

And want to show window or dialog at application startup, before other modules will be loaded.

But! When I close the showed window, the main window, which starts later is closed also!

What am I doing wrong? I've tried to make Showdialog() - the same situation occred.

+2  A: 

The problem you encounter comes from the way, WPF manages the shutdown.

You can change the shutdown-behaviour through the ...

Application.Current.ShutdownMode

... property. Change it to an approriate value:

Application.Current.ShutdownMode = System.Windows.ShutdownMode.OnLastWindowClose;

this will help.

Another way is to set manually the MainWindow-property to your second window.

If you want to make a splashscreen only, use the splashscreen option that is available since .net 3.51. IT has the advantage that it is loaded very early in the application loading sequence, much earlier that a window can.

To do that, open the properties-tab of an image in your project-explorer and set the Build Action to SplashScreen


Update

In one of my apps I had a design that also had to show a modal dialog before showing the main window. At this time I didn't knew about the ShutdownMode-property.

What I did was I first started a Window that was invisible to the user. This was the first window and it also controled app-livetime (default behaviour of WPF). Out from this window I opened the desired dialog (a window that was showed modal). If this dialog has completed unsuccessfull, I terminated the hidden window and the app completetely closed. If the dialog resulted ok, I created the first MainWindow-instance, the user can work with.

HCL
No, both didn't help. But it seems, I've ruled out this situation. I'm Hiding About window and it helps for some reason.
Alex Blokha
I think you have to remove the StartupURI and open the form manually. However what you are describing is also a possible way (I used it also for an alike problem). But only as a suggesion, if your only desire is to show an about information on application startup, think about using the splash-screen-option. It is showed at the very start of the application, before loading the fat assemblies and therefore its use leads to a much better user-experience for your app.
HCL
No, the About dialog is just an example, sorry for misleading. It will be LogonDialog.
Alex Blokha
There is no Start Up URI. First I Show modal LogonDialog , then I manually show splashScreen .I'll close it in OnLoadCompleted. And then I call my MainFormBut here I found another problem OnLoadCompleted doesn't come if I show modal logonDialog and my splashscreen remains on the screen.But I want to show splashScreen after LogonDialog...
Alex Blokha
I have extended my answer, added a paragraph to the solution with the hidden window.
HCL
A: 

private void Application_Startup(object sender, StartupEventArgs e)

{ ShutdownMode mode = this.ShutdownMode;

this.ShutdownMode = System.Windows.ShutdownMode.OnExplicitShutdown;

YourLogonWnd logon = new YourLogonWnd();

logon.ShowDialog();

if (!logon.DialogResult.HasValue || !logon.DialogResult.Value)

this.Shutdown();

else

this.ShutdownMode = mode;

}

In fact, "OnMainWindowClose" is better to use at the end of the event handler.

RPDK