繁体   English   中英

C# 错误 CS024 找不到类型或命名空间名称“Form1”(您是否缺少 using 指令或程序集引用?)

[英]C# Error CS024 The type or namespace name 'Form1' could not be found (are you missing a using directive or an assembly reference?)

我对 C# 相当陌生,尽管我尝试了很多补救措施,但还是遇到了这个错误。 我正在使用表格..

错误指出:

错误 CS0246 找不到类型或命名空间名称“Form1”(您是否缺少 using 指令或程序集引用?)

我的代码看起来像:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace Timer2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

我的设计代码如下所示(Form1 带有红色下划线):

namespace WindowsFormsApp1
{
    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());
        }
    }
}

正如 Hans Passant 提到的,您的Form1类位于Timer2命名空间中。

您可以通过以下任一方式解决此问题

  • 使用完全限定的命名空间来实例化Form1

     Application.Run(new Timer2.Form1());
  • Timer2 namspace 的using语句添加到您的Program

    namespace WindowsFormsApp1 { using Timer2; static class Program {
  • 或更改Form1类中的命名空间

    namespace WindowsFormsApp1 { public partial class Form1 : Form {

    但是你必须确保在Form1.Designer.cs文件中也做同样的事情

就我而言,我有:

引用的 DLL:.NET 4.6

项目:.NET 4.5

由于上述不匹配,4.5 项目无法看到 4.6 DLL 的命名空间内部。 我重新编译了 DLL 以针对 .NET 4.6,我很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM