将 WinForms 应用程序升级到 WPF

发布于 2024-07-11 19:03:31 字数 1263 浏览 5 评论 0原文

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

我这样做时遇到一些问题。 我执行了以下操作(使用 Visual Studio 2008 SP1):

  1. 将目标框架从 2.0 更改为 3.5。
  2. 添加了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):

  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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怎樣才叫好 2024-07-18 19:03:31

我不知道项目文件中的内容会告诉 Visual Studio 正在使用什么类型的项目,但我猜测“ProjectTypeGuids”是关键。

我的 WPF 项目有这个:

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

这是一个链接 我发现它描述了 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:

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

顾挽 2024-07-18 19:03:31

Sailing Judo的答案对我将WinForms项目转换为WPF有用,但没有详细描述需要什么;

  1. 打开要转换的项目的 csproj 文件。
  2. 搜索字符串 - 它很可能不存在。
  3. 在项目文件的标记中添加以下内容。 如果您有多个 PropertyGroup 标记,请使用不带 Condition 属性的标记(例如默认属性)。

    <块引用>

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

  4. 保存 - 如果您打开了项目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;

  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.

谢绝鈎搭 2024-07-18 19:03:31

打开项目清单文件(扩展名为 .csproj 的文件)并修改 PropertyGroup 节点(不带 Condition 元素的文件),如下所示:

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

修改后重新启动 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:

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

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.

一笑百媚生 2024-07-18 19:03:31

可悲的是没有。

WPF 和 Windows 窗体项目不太兼容。

  • 尝试提取尽可能多的挑剔
    将逻辑代码放入类库
  • 创建一个新的空白解决方案
  • 将一个新的 WPF 项目添加到解决方案
  • 将类库项目添加到
    解决方案
  • 将类库设置为依赖项
    WPF 项目
  • 在 WPF 中重新创建窗口

希望这对您有用。

祝您在这次努力中一切顺利!

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!

孤独患者 2024-07-18 19:03:31

将其添加到您的 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}   

Add this to your csproj file:

<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}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文