Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrading a WinForms app to WPF

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.

like image 665
Andy Avatar asked Jan 16 '09 13:01

Andy


People also ask

Is WPF still relevant 2021?

WPF is still one of the most used app frameworks in use on Windows (right behind WinForms).

Is it possible to use Windows form in WPF application?

You can use the WindowsFormsHost to add single Forms into an WPF application.


2 Answers

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.

like image 90
Sailing Judo Avatar answered Sep 20 '22 15:09

Sailing Judo


Sailing Judo's answer worked for me to convert a WinForms project into a WPF, but did not describe in detail what is needed;

  1. Open the csproj file for the project you want to convert.
  2. Search for the string <ProjectTypeGuids> - most likly it will not exist.
  3. Add the following within the tag of the project file. If you have multiple PropertyGroup tags, use the one without a Condition attribute (e.g. the default properties).

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

  4. Save - if you have the project open VS will ask to reload the project.

From what I see and test, I can now easily add WPF Windows and WinForms. My older WinForms also work without problems.

like image 30
Thies Avatar answered Sep 21 '22 15:09

Thies