[英]C# Error CS024 The type or namespace name 'Form1' could not be found (are you missing a using directive or an assembly reference?)
I am fairly new to C# and I run into this error despite the number of remedies I have tried.我对 C# 相当陌生,尽管我尝试了很多补救措施,但还是遇到了这个错误。 I am using a form..我正在使用表格..
The error states:错误指出:
Error CS0246 The type or namespace name 'Form1' could not be found (are you missing a using directive or an assembly reference?)错误 CS0246 找不到类型或命名空间名称“Form1”(您是否缺少 using 指令或程序集引用?)
My code looks like:我的代码看起来像:
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();
}
My design code looks like this (Form1 is underlined with red):我的设计代码如下所示(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());
}
}
}
As Hans Passant mentioned your Form1
class is in the Timer2
namespace.正如 Hans Passant 提到的,您的Form1
类位于Timer2
命名空间中。
You can fix this by either您可以通过以下任一方式解决此问题
use the fully qualified namespace to instaniatate Form1
使用完全限定的命名空间来实例化Form1
Application.Run(new Timer2.Form1());
add an using
statement for the Timer2
namspace to your Program
class将Timer2
namspace 的using
语句添加到您的Program
类
namespace WindowsFormsApp1 { using Timer2; static class Program {
or change the namespace in your Form1
class或更改Form1
类中的命名空间
namespace WindowsFormsApp1 { public partial class Form1 : Form {
but then you have to make sure to do the same in the Form1.Designer.cs file also.但是你必须确保在Form1.Designer.cs文件中也做同样的事情。
In my case I had:就我而言,我有:
Referenced DLL : .NET 4.6引用的 DLL:.NET 4.6
Project : .NET 4.5项目:.NET 4.5
Because of the above mismatch, the 4.5 project couldn't see inside the namespace of the 4.6 DLL.由于上述不匹配,4.5 项目无法看到 4.6 DLL 的命名空间内部。 I recompiled the DLL to target .NET 4.6 and I was fine.我重新编译了 DLL 以针对 .NET 4.6,我很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.