Upgrading a WinForms app to WPF
Asked Answered
D

5

16

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.

Dauphine answered 16/1, 2009 at 13:16 Comment(0)
B
30

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.

Building answered 16/1, 2009 at 15:52 Comment(2)
My project didn't have a ProjectTypeGuids tag, but when I added them from another project (which had the same Guids as yours), that was enough to tell VisualStudio that this is a WPF project now. Thanks! :)Dauphine
Here is another link describing in details how to convert a Winforms project to a WPF project. I just tested it and really seems great. mobilemotion.eu/?p=1537&lang=enAluminate
C
26

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.

Chanukah answered 29/4, 2009 at 14:56 Comment(0)
F
2

Open project manifest file (one with the .csproj extension) and modify PropertyGroup node (the one without Condition element) as below:

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

After this modification restart VS. Now you can add WPF window or other WPF elements. You may also have to add references System.Xaml, PresentationCore and PresentationFramework.

Footlocker answered 16/3, 2020 at 20:3 Comment(0)
P
1

Sadly no.

WPF and Windows Forms projects aren't very compatible.

  • Try to extract as much fussiness logic code into a class library
  • Create a new blank solution
  • Add a new WPF project to the solution
  • Add the class library project to the solution
  • Set the class library as a dependency of the WPF project
  • Recreate the windows in WPF

Hope this works for you.

Best of luck with this endeavor!

Pessimism answered 16/1, 2009 at 13:43 Comment(2)
While that would work, and I am sadly leaning towards going that route, somehow Visual Studio must know that this is a WinForms project, while the other project is a WPF project. How does it know this?Dauphine
Properly something in proj file, but I'm not surePessimism
L
0

Add this to your csproj file:

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

ProjectTypeGuids:

Windows (C#)           {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
Windows (VB.NET)       {F184B08F-C81C-45F6-A57F-5ABD9991F28F}
Windows (Visual C++)   {8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}
Web Application        {349C5851-65DF-11DA-9384-00065B846F21}
Web Site               {E24C65DC-7377-472B-9ABA-BC803B73C61A}
WCF                    {3D9AD99F-2412-4246-B90B-4EAA41C64699}
WPF                    {60DC8134-EBA5-43B8-BCC9-BB4BC16C2548}
XNA (Windows)          {6D335F3A-9D43-41b4-9D22-F6F17C4BE596}
XNA (XBox)             {2DF5C3F4-5A5F-47a9-8E94-23B4456F55E2}
XNA (Zune)             {D399B71A-8929-442a-A9AC-8BEC78BB2433}
Silverlight            {A1591282-1198-4647-A2B1-27E5FF5F6F3B}
ASP.NET MVC            {F85E285D-A4E0-4152-9332-AB1D724D3325}
ASP.NET MVC 4          {E3E379DF-F4C6-4180-9B81-6769533ABE47}
Test                   {3AC096D0-A1C2-E12C-1390-A8335801FDAB}
Solution Folder        {2150E333-8FDC-42A3-9474-1A3956D46DE8}   
Leisurely answered 30/6, 2022 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.