How to change StartupUri of WPF Application?

2019-01-18 20:39发布

问题:

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.

回答1:

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.



回答2:

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 this previous StackOverflow question: WPF ShowDialog returns null immediately on second call



回答3:

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)



回答4:

Just try in OnStartup() :

StartupUri = new Uri("Forms/CustomerEntry.xaml", UriKind.Relative);


标签: wpf uri startup