How to manually create an App.xaml file in a WPF Python Project in Visual Studio?

Robins Gupta picture Robins Gupta · Feb 19, 2014 · Viewed 9.2k times · Source

When we create a new WPF C# Project from Visual Studio we get 4 basic files "App.xaml","App.cs","MainWindow.xaml","MainWindow.cs"

Here App.xaml file is basically used for storing application related Resources in one file to be present all over the application.

But when creating a WPF Python Project in Visual Studio we get only 2 files "MainWindow.xaml" and "MainWindow.py"

I want to create 2 more file in my Python project "App.xaml" and "App.py" so that in "App.xaml" I can easily define my all application related resources styles and control-templates.

Plz Help!!

Answer

Sheridan picture Sheridan · Feb 19, 2014

You can add an Application file to your project from the Solution Explorer. You can press Alt+S to open it. In the Solution Explorer, select your project and then right click and select the Add menu item and then New Item menu item from the popup menu.

Next, in the Add New Item dialog window, you can select WPF from the Installed Templates on the left to filter the available templates. Then, you can simply choose to add a Resource Dictionary and name it App.xaml. As long as you put the relevant code in it, it will be fine:

<Application x:Class="ManuallyCreatingAWpfProject.app"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Startup="AppStartup"
    >
  <Application.Resources>

  </Application.Resources>

</Application>

...

public partial class App : Application
{
    void AppStartup(object sender, StartupEventArgs args)
    {
        Window1 mainWindow = new Window1();
        mainWindow.Show();
    }
}

This code was taken from the Walkthrough: Manually Creating a Windows Presentation Foundation Project Using Visual Studio page on MSDN, which you may also find interesting.

You might also want to go to the project properties (right click on the project in the Solution Explorer and select Properties) and set the Startup Object to the App.xaml file in the Application tab. Finally, you'd better set the Build Action to ApplicationDefinition in the App.xaml file properties.