错误:
The type or namespace name 'Forms' does not exist in the namespace 'System.Windows'
代码:
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();
}
}
}
根据 How do I add assembly references in Visual Studio Code?转到命令面板,然后键入 NuGet:添加新包,然后键入 using System.Windows.Forms 应该可以解决此问题,但未找到任何选项,
我是 .NET 和 C# 的新手,所以这对我的第一个项目来说非常困惑。
最佳答案
这最终归结为 csproj。如果这是一个 .NET 7 项目,那么您可能需要以下三个条目(在项目的 csproj 中的 <PropertyGroup>
中):
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
注意 some 可能已经存在不同的值 - 只需替换它们即可。
关于c# - 找不到命名空间并且无法添加引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75172323/