.net – Upgrading a WinForms app to WPF

netupgradevisual-studio-2008winformswpf

I have a side project that I wrote a few years ago in WinForms. To better help me learn WPF, I would like to rewrite it in WPF. Ideally, I would like to just modify the current project that I have and rewrite the UI in WPF instead of creating a new project.

I'm having some problems doing that. I did the following (using Visual Studio 2008 SP1):

  1. Changed the Target Framework from 2.0 to 3.5.
  2. Added PresentationCore, PresentationFramework, and WindowsBase references.

At this point, I noticed something was amiss. When I right clicked the project and selected Add | New Item, I did not have an option to create a WPF Window – the only WPF class that I could create is a WPF User Control. Since I had some other WPF projects around, I copied App.xaml.* and Window1.xaml.* from that project to mine, and updated them as necessary (basically just changing the namespace of that project to the namespace of my project).

I then deleted Program.cs (which previously contained the Main method, that displayed the main WinForm Form), and built the project. I got an error indicating that there is no Main method.

It seems like internally Visual Studio knows that this project is really a WinForms app, and not a WPF app. In a WPF project (created by selecting "WPF Application"), in the Application properties I can set the instance of the Application class as a start up object. In my WinForms converted project, that isn't an option.

I took a quick look at the .csproj file in a text editor, but I couldn't find anything that would tell Visual Studio that the project is really WinForms rather than WPF.

What else do I need to do to turn my WinForms project into a "real" WPF project? Do I have an option other than creating a new project and just replacing my current project?

Update: I took a closer look at the .csproj files, and I noticed that App.xaml was added as a page:

<Page Include="App.xaml">
  <Generator>MSBuild:Compile</Generator>
  <SubType>Designer</SubType>
</Page>

However, in my other WPF project, it is an ApplicationDefinition. Once I changed that, I could set my Application instance as the start up object, and my app would run. I still don't have an option to create any WPF types other than User Controls, though.

Best Answer

I don't know for certain what in the project file informs Visual Studio what type of project is being used, but my guess is the "ProjectTypeGuids" is key.

My WPF projects have this:

<ProjectTypeGuids>
   {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};
   {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
</ProjectTypeGuids>

Here is a link I found which describes the guids and what kind of project they represent. You'll notice that the guid starting with "60DC" matches the list in the link for WPF applications.