Upgrading a WinForms app to WPF

2019-01-11 10:51发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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!