将 WinForms 应用程序升级到 WPF [英] Upgrading a WinForms app to WPF

查看:19
本文介绍了将 WinForms 应用程序升级到 WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我几年前在 WinForms 中编写的副项目.为了更好地帮助我学习WPF,我想用WPF重写它.理想情况下,我只想修改我拥有的当前项目并在 WPF 中重写 UI,而不是创建一个新项目.

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.

我在这样做时遇到了一些问题.我做了以下(使用 Visual Studio 2008 SP1):

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

  1. 将目标框架从 2.0 更改为 3.5.
  2. 添加了 PresentationCore、PresentationFramework 和 WindowsBase 参考.

此时,我发现有些不对劲.当我右键单击项目并选择添加 |新项目,我没有创建 WPF 窗口的选项 - 我可以创建的唯一 WPF 类是 WPF 用户控件.由于我周围有其他一些 WPF 项目,因此我将 App.xaml.* 和 Window1.xaml.* 从该项目复制到我的项目中,并根据需要更新它们(基本上只是将该项目的命名空间更改为我的项目的命名空间).

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).

然后我删除了 Program.cs(之前包含 Main 方法,显示主 WinForm 窗体),并构建了项目.我收到一个错误,表明没有 Main 方法.

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.

Visual Studio 内部似乎知道这个项目实际上是一个 WinForms 应用程序,而不是一个 WPF 应用程序.在 WPF 项目(通过选择WPF 应用程序"创建)中,在应用程序属性中,我可以将应用程序类的实例设置为启动对象.在我的 WinForms 转换项目中,这不是一个选项.

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.

我在文本编辑器中快速查看了 .csproj 文件,但找不到任何可以告诉 Visual Studio 该项目实际上是 WinForms 而不是 WPF 的内容.

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.

我还需要做什么才能将我的 WinForms 项目变成真正的"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?

更新:我仔细查看了 .csproj 文件,我注意到 App.xaml 被添加为一个页面:

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>

但是,在我的另一个 WPF 项目中,它是一个 ApplicationDefinition.一旦我改变了它,我就可以将我的 Application 实例设置为启动对象,我的应用程序就会运行.不过,我仍然没有选择创建除用户控件以外的任何 WPF 类型.

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.

推荐答案

我不知道确定项目文件中的哪些内容通知 Visual Studio 正在使用的项目类型,但我的猜测是ProjectTypeGuids"是关键.

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.

我的 WPF 项目有这个:

My WPF projects have this:

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

这是一个链接我发现它描述了 guids 和什么样的他们代表的项目.您会注意到以60DC"开头的 guid 与 WPF 应用程序链接中的列表相匹配.

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.

这篇关于将 WinForms 应用程序升级到 WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆