tags:

views:

2254

answers:

3

I am trying to modify App.cs and load the WPF XAML files from code behind but its not working as it should.

No matter whatever I try to set as StartupUri it doesnt start, the program quits after this.

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

        LoginDialog dlg = new LoginDialog();
        if (dlg.ShowDialog() != true)
            return;

        switch (dlg.ChoiceApp) { 
            case ChoiceApp.CustomerEntry:
                StartupUri = new Uri("/MyApp;component/Forms/CustomerEntry.xaml", 
                    UriKind.Relative);
                break;
            case ChoiceApp.VendorEntry:
                StartupUri = new Uri("/MyApp;component/Forms/VendorEntry.xaml", 
                    UriKind.Relative);
                break;
        }
    }
}

Now I even did trace and found out that LoginDialog is working correctly and is returning values correctly but setting "StartupUri" does not work.

I checked in reverse assembly that DoStartup method of App gets called after OnStartup, so technically my StartupUri must load, but it doesnt, in App.xaml startup uri is not at all defined.

Note: Bug Confirmed

I noticed that ShowDialog sets Application.MainWindow and when dialog ends, it sets it back to null, and because of this setting StartupUri does not work after calling Modal Dialog in OnStartup or Startup event.

There is no error or exception about invalid uri or anything like that.

This method works without DialogBox being called in Startup event or OnStartup, i think calling showdialog on this method causes something like its mainwindow being set to expired window and it shuts down after this.

+2  A: 

instead of overriding the OnStartup() method, hook into the event instead.

in the XAML

<Application x:Class="SOTestWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup">
    <Application.Resources>

    </Application.Resources>
</Application>

in the code behind:

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        var rnd = new Random();

        if (rnd.NextDouble() > 0.5)
            StartupUri = new Uri("/SOTestWPF;component/Window1.xaml", UriKind.Relative);
        else
            StartupUri = new Uri("/SOTestWPF;component/Window2.xaml", UriKind.Relative);

    }

This is only my test case and I have verified that it performs correctly (randomly :D)

Alastair Pitts
Sorry, it doesnt work, infact your is random method where else mine is a dialogbox, looks like that dialogbox in Startup event causes problem.
Akash Kava
I just put a MessageBox.Show("test") call in my example and it still worked.I suspect that there is something wrong with your LoginDialog always returning false, or some other error relating inside of the LoginDialog.
Alastair Pitts
Nope, debugging and step through i can see that it is working correctly.
Akash Kava
Well I can't replicate your issue. I can verify that my code is called (albeit randomly), but that the StartupUri is changed programmatically. Try putting a default result for your switch statement and checking to see that it's not falling through there somehow.
Alastair Pitts
I did as Joel said, it is working, but somehow I feel its a bug, I set the Dialog as startup and then loaded window based on Dialog result on click of OK button.
Akash Kava
+1  A: 

Do you still have a StartupUri specified in the XAML? If so, remove it and see if that helps.MSDN Source

If not, you may need to approach this differently: have your Dialog as your startup, then from that point open another Window based on the selected value.

Joel Cochran
I did exactly as you said, however this is some workaround, but from application design point of view, setting up StartupUri should work and that looks little nice code, which is easy to document and explain. Thanks for your suggestion.
Akash Kava
+1  A: 

Akash, I ran into this exactly issue trying to implement a LoginDialog just like yours. The dialog does not have a bug, but rather the behavior is by design.

Not a bug. The default ShutdownMode of Application is OnLastWindowClosed, so as soon as the first window is closed your application will start shutting down! Change to OnExplicitShutdown and it will work, but you'll have to manage the shutdown.

See http://stackoverflow.com/questions/1243833/wpf-showdialog-returns-null-immediatly-on-second-call

tofutim
Wow, i didnt know this, great answer man, I will try to use this and see if it works.
Akash Kava