将 WinForms 应用程序升级到 WPF
我有一个几年前在 WinForms 中编写的副项目。 为了更好的帮助我学习WPF,我想用WPF重写它。 理想情况下,我只想修改当前的项目并在 WPF 中重写 UI,而不是创建新项目。
我这样做时遇到一些问题。 我执行了以下操作(使用 Visual Studio 2008 SP1):
- 将目标框架从 2.0 更改为 3.5。
- 添加了PresentationCore、PresentationFramework 和WindowsBase 参考。
此时,我注意到有些不对劲。 当我右键单击该项目并选择“添加”| 新项目,我没有创建 WPF 窗口的选项 - 我可以创建的唯一 WPF 类是 WPF 用户控件。 由于我还有其他一些 WPF 项目,因此我将 App.xaml.* 和 Window1.xaml.* 从该项目复制到我的项目中,并根据需要更新它们(基本上只是将该项目的命名空间更改为我的项目的命名空间)。
然后,我删除了 Program.cs(之前包含显示主 WinForm 窗体的 Main 方法),并构建了该项目。 我收到一个错误,表明没有 Main 方法。
看起来 Visual Studio 内部知道这个项目实际上是一个 WinForms 应用程序,而不是一个 WPF 应用程序。 在 WPF 项目(通过选择“WPF 应用程序”创建)中,在应用程序属性中,我可以将应用程序类的实例设置为启动对象。 在我的 WinForms 转换项目中,这不是一个选项。
我在文本编辑器中快速查看了 .csproj 文件,但找不到任何可以告诉 Visual Studio 该项目实际上是 WinForms 而不是 WPF 的内容。
我还需要做什么才能将我的 WinForms 项目变成“真正的”WPF 项目? 除了创建新项目并替换当前项目之外,我还有其他选择吗?
更新:我仔细查看了 .csproj 文件,发现 App.xaml 被添加为页面:
<Page Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
但是,在我的其他 WPF 项目中,它是一个 ApplicationDefinition。 一旦我改变了这一点,我就可以将我的应用程序实例设置为启动对象,然后我的应用程序就会运行。 不过,我仍然无法选择创建除用户控件之外的任何 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.
I'm having some problems doing that. I did the following (using Visual Studio 2008 SP1):
- Changed the Target Framework from 2.0 to 3.5.
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不知道项目文件中的内容会告诉 Visual Studio 正在使用什么类型的项目,但我猜测“ProjectTypeGuids”是关键。
我的 WPF 项目有这个:
这是一个链接 我发现它描述了 guid 和他们代表什么样的项目。 您会注意到以“60DC”开头的 guid 与 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.
My WPF projects have this:
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.
Sailing Judo的答案对我将WinForms项目转换为WPF有用,但没有详细描述需要什么;
csproj
文件。
- 它很可能不存在。在项目文件的标记中添加以下内容。 如果您有多个
PropertyGroup
标记,请使用不带Condition
属性的标记(例如默认属性)。<块引用>
保存 - 如果您打开了项目VS 将要求重新加载项目。
根据我的观察和测试,我现在可以轻松添加 WPF Windows 和 WinForms。 我的旧版 WinForm 也可以正常工作。
Sailing Judo's answer worked for me to convert a WinForms project into a WPF, but did not describe in detail what is needed;
csproj
file for the project you want to convert.<ProjectTypeGuids>
- most likly it will not exist.Add the following within the tag of the project file. If you have multiple
PropertyGroup
tags, use the one without aCondition
attribute (e.g. the default properties).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.
打开项目清单文件(扩展名为 .csproj 的文件)并修改 PropertyGroup 节点(不带 Condition 元素的文件),如下所示:
修改后重新启动 VS。 现在您可以添加 WPF 窗口或其他 WPF 元素。 您可能还需要添加引用 System.Xaml、PresentationCore 和PresentationFramework。
Open project manifest file (one with the .csproj extension) and modify PropertyGroup node (the one without Condition element) as below:
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.
可悲的是没有。
WPF 和 Windows 窗体项目不太兼容。
将逻辑代码放入类库
解决方案
WPF 项目
希望这对您有用。
祝您在这次努力中一切顺利!
Sadly no.
WPF and Windows Forms projects aren't very compatible.
logic code into a class library
solution
the WPF project
Hope this works for you.
Best of luck with this endeavor!
将其添加到您的 csproj 文件:
ProjectTypeGuids:
Add this to your csproj file:
ProjectTypeGuids: