Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change StartupUri of WPF Application?

Tags:

uri

wpf

startup

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.

like image 626
Akash Kava Avatar asked Dec 22 '09 11:12

Akash Kava


People also ask

What is StartupUri WPF?

StartupUri property gets or sets a UI that is automatically shown when an application starts. You can change StartupUri of an application to a window you want to display when your application starts. <Application x:Class="WPFSample.App"

How do I change the startup window in WPF?

If you look at App. xaml class of your WPF application, you will see the following XAML code. Here the StartupUri sets the startup Window of an application. If you want to change the Startup window to some other window, just change this value.

Can we convert WPF in web application?

If you want to convert your WPF app to ASP.NET Web forms or MVC you would have to do it manually.To make it easier you could create shared code which can be used by both WPF and the web application. Thanks for your reply.In our WPF project we have used MVC pattern.

How does a WPF application start?

The first Window that is instantiated within a WPF application is automatically set by Application as the main application window. The main window is referenced with the Application. MainWindow property.


1 Answers

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

like image 169
tofutim Avatar answered Sep 23 '22 10:09

tofutim