7

I'm trying to run a window, close it, and then run a second window, in a similar way that seems to work with Windows Forms.

namespace WpfApplication1
{

public partial class App : Application
{
    [STAThread]
    public static void Main()
    {
        Application app = new Application();
        //windowMain.Show();
        app.ShutdownMode = ShutdownMode.OnExplicitShutdown;
        MainWindow windowMain = new MainWindow();
        app.Run(windowMain);
        Window1 window1 = new Window1();
        window1.Show();
        app.Run(window1);
    }
}
}

I've set the Build Action in the App.xaml properties from ApplicationDefinition to Page, but the programme throws an exception when window1 is initialised. What am I doing wrong?

Edit: I've modified the xaml in App.xaml as suggested by first answer and edited main as suggested by the comment.

<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
ShutdownMode="OnExplicitShutdown">
<Application.Resources>
</Application.Resources>
</Application>

but I'm still getting the exception.

4
  • What is the exception that gets thrown?
    – Matt West
    May 7, 2011 at 20:33
  • thrown on: Window1 window1 = new Window1(); An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll Additional information: The Application object is being shut down. May 7, 2011 at 20:51
  • The way your code works, you're not using App.xaml at all, so changing it will have no effect. You're instantiating the framework-provided Application, not your own App class.
    – svick
    May 7, 2011 at 20:57
  • Ok @svick cheers, I have inserted the app.Shutdown line into Main(), its got rid of the exception, but I still can't see window1. May 8, 2011 at 11:18

3 Answers 3

9

OK this is what I've divined so far. The Solution Builder looks for a Main() function. Why its not a WinMain() function I'm still not a hundred per cent clear on. If there is no Main(), you get an error. You can have more than one Main() as long as the Project properties: "Application" page/tab: property: "StartUp Object" is set to point to one of the main()s. This is done from an automatically created drop down list.

When a “WPF Application” project is created, Visual Studio(VS) create an xaml file called “App.xaml”. This is a class declaration where “App” is derived from the “Application” Class. VS also automatically generates hidden files for an xaml file. It creates a “name.g.i.cs” file, when the xaml file is created. It creates a “name.g.cs” file the first time the project is built after the creation of the xaml file. In this case it creates “App.g.cs” and “App.g.i.cs”. These files are hidden by default. To view them, press the “Show all files” button at the top of the Solution Explorer, they can be found in “\ obj\86\Debug” folder. When you delete an xaml file the “name.g.i.cs” and the “name.g.cs” files remain and are not deleted.

The “App.xaml” file’s “build Action” property is set to “Application Definition” when created by VS. When this property is set to “Application Definition” a Main() function is automatically created in “name.g.i.cs”:

    [System.STAThreadAttribute()]
    [System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static void Main()
{
    WpfApplication8.App app = new WpfApplication8.App();
    app.InitializeComponent();
    app.Run();
}

When this property is set to “Page”, the Main() function is automatically removed by VS. You can create new “Application” derived classes in code or in xaml. I haven’t found a neat way to do it in xaml. There doesn’t seem to be a template for an xaml “Application” derived class. I created a “.cs” code file and then renamed it to an .xaml file. For some reason VS won’t allow you to have more than one xaml “Application” declaration file set to “Application Build”, it doesn’t even give you the option of choosing one in the "Project: Properties: Application": “Startup Object” property.

As you can see in the hidden Main(), an instance of “App” is instantiated and run. If using your own Main() function: an instance of, the base “Application” class, or an “Application” derived class (whether declared in code or in xaml), can be declared and run. The “Application” class should only be instantiated once and should only be run once. If the “Application” derived class is declared in xaml then a simple application can be run by using the StartUpUri property in the xaml file: StartupUri="Windowname.xaml". Alternatively the top level UI programme logic can be placed in a Startup event handler. If “Startup="Application_Startup" is placed in the “App.xaml” file then an event handler can be written:

private void Application_Startup(object sender, StartupEventArgs e)
{
    MainWindow windowMain = new MainWindow();
    windowMain.ShowDialog();
    Window1 window1 = new Window1();
    window1.ShowDialog();
    Shutdown();
}

You have to use ShowDialog() here, because it blocks until the window is closed. If you used Show() instead, it would show one window, then immediately show the other one and shutdown the application. In this case there's no need to call the Run() method yourself, that's done automatically. The “Application” class instance can be run in code whether its declared in code or in xaml. You can then perform initialisation code prior to calling Run(). This would be placed in the Application_ Startup() event handler using the other way. However, if the “Application.Run” call is ever made in the programme, then no windows should be opened (using show() or ShowDialog()) in Main() or anywhere outside of the Application Class or within events and functions called from those events, called during “Application.Run()”.

The Application class has a ShutdownMode property (Application.ShutdownMode). The default for this is: “OnMainWindowClose”. This property can also be set to “OnLastWindowClose” or “OnExplicitShutdown” in code or in the xaml. You will need to reset this if you don't want the programme to close down when the MainWindow is closed.

I think for my purposes it is better not to use the Application class at all and just call the windows using Show() and “ShowDialog()”. This way I can use WPF pages but I could also call Windows Forms, or DirectX screens, as long as they are not open at the same time, or have no UI at all, if the programme is running remotely. Is there any reason for not doing it this way?

8

I think your application is shuting down when you close the first window. You need to set Application.ShutdownMode to OnExplicitShutdown.

2
  • I've tried that and changed the xaml in App.xaml, don't seem to be able to display it properly on the forum. May 7, 2011 at 20:53
  • Really? You added a line of code like this: app.ShutdownMode = ShutdownMode.OnExplicitShutdown after your initial creation of app?
    – Matt West
    May 7, 2011 at 20:55
2

If all you want to do is to show one window, when that closes, show another and when that closes, shutdown the whole application, you should keep the Build action as ApplicationDefinition, set ShutdownMode to OnExplicitShutdown (probably in App.xaml, but you can it in code-behind too) and put the following code in an event handler of the Startup event of your application:

private void Application_Startup(object sender, StartupEventArgs e)
{
    MainWindow windowMain = new MainWindow();
    windowMain.ShowDialog();
    Window1 window1 = new Window1();
    window1.ShowDialog();
    Shutdown();
}

You have to use ShowDialog() here, because it blocks until the window is closed. If you used Show() instead, it would show one window, then immediatelly show the other one and shutdown the application.

There's no need to run the Run() method yourself, that's done automatically.

3
  • I put the event handler inside the App class, No exception is thrown but I still can't see window1. It seems I can only see MainWindow because the startup URI is set to that in "App.xaml". May 8, 2011 at 11:09
  • 1
    You also have to set up the event, put Startup="Application_Startup" in in your App.xaml. And remove the startup URI.
    – svick
    May 8, 2011 at 11:13
  • Ah Thanks, that works. I've been trying to create a strategy game application. I'm trying to separate the User Interface from the core functionality. So that ultimately a completely different UI could be plugged in, or even have the application run without any UI and only interface with remote UI's on different machines. So I'm trying to utilise WPF, without structuring my high level programme logic around WPF. So I was trying to use a main() declaration, so the programme could easily be converted to a winform or a console application or simple windows application. May 8, 2011 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.