找不到命名空间且无法添加引用

1 .net c#

错误:

命名空间“System.Windows”中不存在类型或命名空间名称“Forms”

代码:

using System;
using System.Windows.Forms;

namespace SimpleCounter
{
    public partial class Form1 : Form
    {
        int counter = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            counter++;
            lblCounter.Text = counter.ToString();
        }

        private void btnSubtract_Click(object sender, EventArgs e)
        {
            counter--;
            lblCounter.Text = counter.ToString();
        }

        private void btnGolden_Click(object sender, EventArgs e)
        {
            counter += 2;
            lblCounter.Text = counter.ToString();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

根据如何在 Visual Studio Code 中添加程序集引用?转到命令选项板,然后输入 NuGet: Add New Package,然后使用 System.Windows.Forms 键入应该可以解决此问题,但找不到任何选项,

我是 .NET 和 C# 的新手,所以这对于我的第一个项目来说非常令人困惑。

Mar*_*ell 5

这最终取决于 csproj。如果这是一个 .NET 7 项目,那么您可能需要以下三个条目(在<PropertyGroup>项目的 csproj 中):

    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
Run Code Online (Sandbox Code Playgroud)

请注意,有些可能已经存在且具有不同的值 - 只需替换它们即可。