变量 'control' 未声明或从未分配 - 除非是,应用程序编译

The variable 'control' is either undeclared or was never assigned - Except it is, and the application compiles

我的设计器视图今天开始出现此错误。但是,应用程序编译没有错误并且控件可用。

自定义控件在设计器中定义正确。如果我单击 "ignore and continue",设计器可以正常加载,但缺少提到的控件。

到目前为止我已经尝试过:

清理、重建然后重新启动 visual studio 没有奏效。

自从昨天使用它以来,我没有做任何改变,然后今天走进办公室。

我的构造函数是

public CalendarWindow(MainWindow owner)
{
    InitializeComponent();
    this._owner = owner;
}

public CalendarWindow(int job, int visit, DataTable customer, DataTable address)
{

    InitializeComponent();

    this.addressDetails = address;
    this.customerDetails = customer;
    this.visitID = visit;
    jobID = job;
}

并且在 designer.cs 中的 InitializeComponent() 中声明了控件

// 
// panel1
// 
this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
| System.Windows.Forms.AnchorStyles.Left) 
| System.Windows.Forms.AnchorStyles.Right)));
this.panel1.AutoScroll = true;
this.panel1.Controls.Add(this.label5);
this.panel1.Controls.Add(this.label4);
this.panel1.Controls.Add(this.calendar4); \designer "error" points to here
this.panel1.Controls.Add(this.label3);
this.panel1.Controls.Add(this.calendar3); \designer "error" points to here
this.panel1.Controls.Add(this.calendar2); \designer "error" points to here    
this.panel1.Controls.Add(this.calendar1); \designer "error" points to here
this.panel1.Controls.Add(this.label2);
this.panel1.Location = new System.Drawing.Point(218, 12);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(873, 628);
this.panel1.TabIndex = 10;
this.panel1.MouseDown += new 
System.Windows.Forms.MouseEventHandler(this.calendar1_MouseDown);

看来我已经成功解决了这个问题。

虽然编译器在编译代码和 运行 应用程序时没有任何问题,但它不喜欢控件本身中的某些代码。

我只是设法抓住了这个,因为我试图在点击 "ignore and continue" 后向表单添加相同的控件,此时它拒绝添加它并为我提供了导致问题的方向,这碰巧在 class 中,它不是控件的一部分,但被它使用了。

更正该错误解决了问题。

我不明白为什么它会编译 运行 没有问题,但反馈的错误没有提到有问题的工件。

我遇到了同样的问题,我已经解决了。实际上 Visual Studio 仅适用于 X86 控件,您无法在 X64 模式下创建用户控件并使用它。

您应该在任何 CPU 模式下添加一个新的 class 库并构建该库。然后你可以在你的项目中添加它的 DLL 并且它可以工作。

如果不是,您必须转到配置管理器并将 "Active solution platform" 设置为 X64,同时对所有子项目执行此操作。 请记住,必须检查构建选项。 然后转到 class 库的属性并单击构建选项卡。然后将 "platform target" 设置为 Any CPU.