简体   繁体   English

将 WinForms 应用程序升级到 WPF

[英]Upgrading a WinForms app to WPF

I have a side project that I wrote a few years ago in WinForms.我有一个几年前在 WinForms 中编写的副项目。 To better help me learn WPF, I would like to rewrite it in WPF.为了更好地帮助我学习 WPF,我想用 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.理想情况下,我只想修改我拥有的当前项目并在 WPF 中重写 UI,而不是创建一个新项目。

I'm having some problems doing that.我在这样做时遇到了一些问题。 I did the following (using Visual Studio 2008 SP1):我做了以下(使用 Visual Studio 2008 SP1):

  1. Changed the Target Framework from 2.0 to 3.5.将目标框架从 2.0 更改为 3.5。
  2. Added PresentationCore, PresentationFramework, and WindowsBase references.添加了 PresentationCore、PresentationFramework 和 WindowsBase 引用。

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.新项目,我没有创建 WPF 窗口的选项 - 我可以创建的唯一 WPF 类是 WPF 用户控件。 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).由于我周围还有其他一些 WPF 项目,因此我将 App.xaml.* 和 Window1.xaml.* 从该项目复制到我的项目中,并根据需要对其进行了更新(基本上只是将该项目的名称空间更改为我的项目的名称空间)。

I then deleted Program.cs (which previously contained the Main method, that displayed the main WinForm Form), and built the project.然后我删除了 Program.cs(之前包含 Main 方法,显示主 WinForm 窗体),并构建了项目。 I got an error indicating that there is no Main method.我收到一个错误,表明没有 Main 方法。

It seems like internally Visual Studio knows that this project is really a WinForms app, and not a WPF app.似乎在内部 Visual Studio 知道该项目实际上是一个 WinForms 应用程序,而不是 WPF 应用程序。 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.在 WPF 项目中(通过选择“WPF 应用程序”创建),在应用程序属性中,我可以将应用程序类的实例设置为启动对象。 In my WinForms converted project, that isn't an option.在我的 WinForms 转换项目中,这不是一个选项。

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.我在文本编辑器中快速查看了 .csproj 文件,但找不到任何可以告诉 Visual Studio 该项目实际上是 WinForms 而不是 WPF 的东西。

What else do I need to do to turn my WinForms project into a "real" WPF project?我还需要做什么才能将我的 WinForms 项目变成“真正的”WPF 项目? 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:更新:我仔细查看了 .csproj 文件,发现 App.xaml 被添加为页面:

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

However, in my other WPF project, it is an ApplicationDefinition.但是,在我的其他 WPF 项目中,它是一个 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.不过,除了用户控件之外,我仍然没有创建任何 WPF 类型的选项。

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.不确定项目文件中的哪些内容会告知 Visual Studio 正在使用哪种类型的项目,但我的猜测是“ProjectTypeGuids”是关键。

My WPF projects have this:我的 WPF 项目有这个:

<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.您会注意到以“60DC”开头的 guid 与 WPF 应用程序链接中的列表相匹配。

Sailing Judo's answer worked for me to convert a WinForms project into a WPF, but did not describe in detail what is needed; Sailing Judo 的回答对我有用,可以将 WinForms 项目转换为 WPF,但没有详细描述需要什么;

  1. Open the csproj file for the project you want to convert.打开要转换的项目的csproj文件。
  2. Search for the string <ProjectTypeGuids> - most likly it will not exist.搜索字符串<ProjectTypeGuids> - 很可能它不存在。
  3. Add the following within the tag of the project file.在项目文件的标记中添加以下内容。 If you have multiple PropertyGroup tags, use the one without a Condition attribute (eg the default properties).如果您有多个PropertyGroup标签,请使用没有Condition属性的标签(例如默认属性)。

    <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.保存 - 如果您打开了项目,VS 会要求重新加载项目。

From what I see and test, I can now easily add WPF Windows and WinForms.从我看到和测试的结果来看,我现在可以轻松添加 WPF WindowsWinForms。 My older WinForms also work without problems.我的旧 WinForms 也可以正常工作。

Sadly no.可悲的是没有。

WPF and Windows Forms projects aren't very compatible. WPF 和 Windows 窗体项目不是很兼容。

  • 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将新的 WPF 项目添加到解决方案
  • Add the class library project to the solution将类库项目添加到解决方案
  • Set the class library as a dependency of the WPF project将类库设置为 WPF 项目的依赖项
  • Recreate the windows in WPF在 WPF 中重新创建窗口

Hope this works for you.希望这对你有用。

Best of luck with this endeavor!祝你好运!

Open project manifest file (one with the .csproj extension) and modify PropertyGroup node (the one without Condition element) as below:打开项目清单文件(一个带有 .csproj 扩展名的文件)并修改 PropertyGroup 节点(没有 Condition 元素的那个),如下所示:

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

After this modification restart VS.修改后重启VS。 Now you can add WPF window or other WPF elements.现在您可以添加 WPF 窗口或其他 WPF 元素。 You may also have to add references System.Xaml, PresentationCore and PresentationFramework.您可能还必须添加引用 System.Xaml、PresentationCore 和 PresentationFramework。

Add this to your csproj file:将此添加到您的 csproj 文件中:

<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}   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM