繁体   English   中英

有没有办法将我的控制台应用程序转换为C#中的Windows窗体应用程序?

[英]Is there a way to convert my Console Application into a Windows Forms Application in C#?

我创建了一个控制台应用程序,但我想把它变成一个Windows窗体应用程序。

我发现似乎是我需要的,但是当我尝试使用System.Windows.Forms时,我收到了一条错误消息;

这是我收到的错误消息:

错误1命名空间“系统”中不存在类型或命名空间名称“Windows”(您是否缺少程序集引用?)

还有另一个步骤,还是在VS 2008中有所不同?

您需要添加对WinForms程序集的引用

  • 右键单击解决方案并选择“添加引用”
  • 选择System.Windows.Forms并单击OK

您可能需要对System.Data执行相同的操作,具体取决于您的项目设置

确保在项目的引用中添加System.Windows.Forms程序集。 在解决方案资源管理器中,右键单击“引用”,然后在“.NET”选项卡下找到System.Windows.Forms程序集并添加它。

您需要添加对System.Windows.Forms的引用。 右键单击项目,然后选择“添加引用”。

在.NET选项卡上,选择前面提到的参考。

最简单的方法:

从.Net Core 3开始,最简单的方法是在磁盘上的某个位置备份Program.cs (或者只使用git)并运行Program.cs所在的以下命令:

dotnet new winforms --force

它将取代您的Program.cs.csproj 然后只需从旧的Program.cs复制所需的代码。 而已!

要手动转换项目,这可能会有所帮助:

以下是.Net Core 3项目的外观(使用dotnet new winforms生成):

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

<PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

</Project>

以下是Program.cs查找新项目的方式:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LinkInterceptor
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

暂无
暂无

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

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