c# - 找不到命名空间并且无法添加引用

标签 c# .net

错误:

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/

相关文章:

c# - 如何在多个 perlin 噪声 block 之间平滑?

c# - 通过 C# 确定字符串是否为有效文件路径

c# - 通过 OleDbConnection 获取单个记录的最佳方法是什么?

c# - 当对象为空时 Linq to 对象 VS Linq to SQL

c# - Xceed Datagrid - 获取过滤器行字符串

c# - 如何在 Unity2D 中以编程方式切换图 block 调色板

c# - 将 WebApi 发布到子文件夹

c# - 将电子邮件编码为 UTF8

c# - 我可以创建一个 SqlDataReader 并为其提供值,而不必从另一个对象返回 SqlDataReader 吗? SqlCommand.ExecuteReader()

c# - 具有 Entity Framework 上下文的devart web api调用过程