簡體   English   中英

找不到命名空間並且無法添加引用

[英]Namespace connot be found and reference can't be added

錯誤:

命名空間“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();
        }
    }
}

根據如何在 Visual Studio Code 中添加程序集引用? 轉到命令面板,然后鍵入 NuGet:添加新的 Package,然后使用 System.Windows.Forms 鍵入應該可以解決此問題,但未找到任何選項,

我是 .NET 和 C# 的新手,所以這對我的第一個項目來說非常混亂。

這最終歸結為 csproj。 如果這是一個 .NET 7 項目,那么您可能需要以下三個條目(在項目的 csproj 中的<PropertyGroup>中):

    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>

請注意,有些可能已經存在不同的值 - 只需替換它們即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM