【问题标题】:Is there a way to convert my Console Application into a Windows Forms Application in C#?有没有办法将我的控制台应用程序转换为 C# 中的 Windows 窗体应用程序?
【发布时间】:2010-03-03 18:55:24
【问题描述】:

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

我找到了This,它似乎是我需要的,但是当我尝试使用 System.Windows.Forms 时收到错误消息;

这是我收到的错误消息:

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

还有其他步骤,还是在 VS 2008 中有所不同?

【问题讨论】:

    标签: c# visual-studio winforms console-application


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢。这行得通。而对于系统数据,您的意思是添加对它的引用?
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      您需要添加对System.Windows.Forms 的引用。右键单击您的项目并选择添加引用。

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

      【讨论】:

        【解决方案4】:

        最简单的方法:

        从 .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());
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-04-22
          • 2011-10-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-09-24
          • 1970-01-01
          相关资源
          最近更新 更多