c# - C#错误CS024找不到类型或 namespace 名称 'Form1'(您是否缺少using指令或程序集引用?)

标签 c# compiler-errors

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

错误状态:

Error CS0246 The type or namespace name 'Form1' could not be found (are you missing a using directive or an assembly reference?)



我的代码如下:
    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 mentioned,您的Form1类位于Timer2命名空间中。

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

  • 使用完全限定的 namespace 来实例化Form1
    Application.Run(new Timer2.Form1());
    
  • using命名空间的Timer2语句添加到Program
    namespace WindowsFormsApp1
    {
        using Timer2;
    
        static class Program
        {
    
  • 或更改Form1类中的 namespace
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
    

    但是然后您必须确保在Form1.Designer.cs文件中也执行相同的操作。
  • 关于c# - C#错误CS024找不到类型或 namespace 名称 'Form1'(您是否缺少using指令或程序集引用?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46658589/

    相关文章:

    c# - Hololens v2 与 Android 智能手机之间的通信

    C# - 如何检查用户是否使用 HttpWebRequest 登录到 Web

    c# - 这是错误使用泛型的例子吗?

    c# - 将程序集添加到gac时csharpcodeprovider无法编译

    java - 将具有外部依赖关系的.java文件编译为.class文件

    java - 更新到 Android 3.0 和 SDK 27 后的错误

    c# - LINQ 如何根据给定条件对项目进行排序?

    c# - 在 Entity Framework Core 中保存 HashSet<string>

    java - java : 'cannot find symbol' if not explicitly imported 中的自定义异常

    java - 类分配命理报告Java代码