升级WinForms应用程序到WPF应用程序、WinForms、WPF

2023-09-02 21:33:17 作者:书生夺命剑

我有一个侧项目,我的WinForms几年前写的。为了更好地帮助我学习WPF中,我想重写在WPF。理想情况下,我想只修改当前的项目,我已经和重写创建一个新项目的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.

我有一些问题,这样做。我做了以下(使用Visual Studio 2008 SP1):

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

改变目标框架,从2.0到3.5。 新增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中(其中previously包含的主要方法,即显示主winform形式),并建项目。我得到一个错误,指出没有主要方法。

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。有一次,我改变了这一切,我可以把我的应用实例作为启动对象,我的应用程序将运行。我还没有一个选项来创建任何类型的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>

这里是一个链接我发现它描述的GUID,什么样的项目,他们重新present。你会发现,以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.