Share via


Walkthrough: Manually Creating a Windows Presentation Foundation Project Using Visual Studio

This walkthrough shows how to use Visual Studio to create a WPF project manually. This procedure is useful if you do not have the Development Tools for .NET Framework 3.0 installed. For information on installing these tools, see Installation Instructions for the Windows SDK.

Tasks illustrated in this walkthrough include:

  • Creating the project.

  • Using Visual Studio to create WPF files.

  • Defining WPF content.

  • Manually editing the project file.

For a complete code listing of the tasks illustrated in this walkthrough, see Manually Creating a Windows Presentation Foundation Project Sample.

When you are finished, you will be able to create a WPF project manually from a Windows Forms project.

NoteNote:

The dialog boxes and menu commands you see might differ from those described in Help, depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu.

Creating the Project

The first step is to create the application project. You will modify this project to handle WPF content.

To create the project

  1. Create a Windows Forms application project named ManuallyCreatingAWpfProject.

  2. In Solution Explorer, add references to the following WPF assemblies.

    • PresentationCore

    • PresentationFramework

    • WindowsBase

    The default location for these assemblies is %programfiles%\Reference Assemblies\Microsoft\Framework\v3.0.

  3. Delete Program.cs from the project. The program's entry point will be defined in the WPF application file.

Using Visual Studio to Create Windows Presentation Foundation Files

To use Visual Studio to create Windows Presentation Foundation files

  1. In Solution Explorer, right-click ManuallyCreatingAWpfProject, point to Add, and then click New Item. In the Add New Item dialog box, select Text File, name the new file Window1.xaml, and click Add. This file will hold the code for the WPF user interface (UI).

  2. Right-click ManuallyCreatingAWpfProject again, point to Add, and then click New Item. In the Add New Item dialog box, select Class, name the new file Window1.xaml.cs, and click Add.

  3. Right-click ManuallyCreatingAWpfProject, point to Add, and then click New Item. In the Add New Item dialog box, select Text File, name the new file MyApp.xaml, and click Add. This file will hold the WPF application definition.

  4. Right-click ManuallyCreatingAWpfProject, point to Add, and then click New Item. In the Add New Item dialog box, select Class, name the new file MyApp.xaml.cs, and click Add.

Defining Windows Presentation Foundation Content

Define the content for the WPF-based application by copying the following code into the newly created files.

To define Windows Presentation Foundation content

  1. In Solution Explorer, double-click Window1.xaml to open it in the Code Editor.

  2. Paste the following code into Window1.xaml.

    <Window x:Class="ManuallyCreatingAWpfProject.Window1"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        >
    
        <Canvas>
          <Button Content="Simple Control"/>
        </Canvas>
    </Window>
    

    This code creates a simple window that displays a Button control.

  3. In Solution Explorer, select Window1.xaml to view its properties. In the Properties window, change the file's Encoding property to UTF-8.

  4. In Solution Explorer, double-click Window1.xaml.cs to open it in the Code Editor.

  5. Paste the following code into Window1.xaml.cs.

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Media;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace ManuallyCreatingAWpfProject
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
    
        public partial class Window1 : Window
        {
            public Window1()
            {
               InitializeComponent();
            }
        }
    }
    
  6. In Solution Explorer, double-click MyApp.xaml to open it in the Code Editor.

  7. Paste the following code into MyApp.xaml.

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

    This code defines the WPF Application.

  8. In Solution Explorer, select MyApp.xaml to view its properties. In the Properties window, change the file's Encoding property to UTF-8.

  9. In Solution Explorer, double-click MyApp.xaml.cs to open it in the Code Editor.

  10. Paste the following code into MyApp.xaml.cs.

    using System;
    using System.Windows;
    using System.Data;
    using System.Xml;
    using System.Configuration;
    
    namespace ManuallyCreatingAWpfProject
    {
        /// <summary>
        /// Interaction logic for app.xaml
        /// </summary>
    
        public partial class app : Application
        {
            void AppStartup(object sender, StartupEventArgs args)
            {
                Window1 mainWindow = new Window1();
                mainWindow.Show();
            }
        }
    }
    

Manually Editing the Project File

You must edit the project file manually to make it compatible with the WPF build process.

To manually edit the project file

  1. In Solution Explorer, right-click ManuallyCreatingAWpfProject and click Unload Project.

    The project name is labeled (unavailable).

  2. Right-click ManuallyCreatingAWpfProject, and click Edit ManuallyCreatingAWpfProject.csproj.

    The project file opens in the Code Editor.

  3. Find the following line.

    <None Include="Window1.xaml" />
    
  4. Change this line as follows.

    <Page Include="Window1.xaml" />
    
  5. Find the following line.

    <None Include="MyApp.xaml" />
    
  6. Change this line as follows.

    <ApplicationDefinition Include="MyApp.xaml" />
    
  7. Find the following line.

    <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
    
  8. After that line, add the following line.

    <Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />
    
  9. Save the project file and close it.

  10. In Solution Explorer, right-click ManuallyCreatingAWpfProject and click Reload Project.

  11. Press F5 to build and run the application.

See Also

Reference

ElementHost
WindowsFormsHost

Other Resources

Manually Creating a Windows Presentation Foundation Project Sample
Migration and Interoperability How-to Topics