【问题标题】:Type 'usercontrol' is not defined (Visual Studio 2015)未定义类型“用户控件”(Visual Studio 2015)
【发布时间】:2016-10-01 20:24:40
【问题描述】:

我在 VS2015 的项目中添加了一个自定义 winforms 用户控件。当我将控件拖到窗体并编译时,我得到错误类型'xxx'未定义。如果我从控件名称中删除命名空间,我可以修复,但是对用户控件的任何更改都会覆盖我的更改。 有没有办法防止这种情况发生?

这是自动生成的控件(称为“ctrlTestStatus”):

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class ctrlTestStatus
    Inherits System.Windows.Forms.UserControl

    'UserControl overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()>
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()>
    Private Sub InitializeComponent()
        Me.TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel()
        Me.prgLocalDatabase = New System.Windows.Forms.ProgressBar()
        Me.prgRemoteDatabase = New System.Windows.Forms.ProgressBar()
        Me.lblLocalDatabaseStatus = New System.Windows.Forms.Label()
        Me.lblRemoteDatabaseStatus = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.prgAmazon = New System.Windows.Forms.ProgressBar()
        Me.Label6 = New System.Windows.Forms.Label()
        Me.lblAmazonStatus = New System.Windows.Forms.Label()
        Me.prgEbay = New System.Windows.Forms.ProgressBar()
        Me.lblEbayStatus = New System.Windows.Forms.Label()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.lbStatus = New System.Windows.Forms.ListBox()
        Me.TableLayoutPanel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'TableLayoutPanel1
        '
        Me.TableLayoutPanel1.BackColor = System.Drawing.SystemColors.Window
        Me.TableLayoutPanel1.ColumnCount = 3
        Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25.30766!))
        Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 58.95527!))
        Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 15.73707!))
        Me.TableLayoutPanel1.Controls.Add(Me.prgLocalDatabase, 1, 0)
        Me.TableLayoutPanel1.Controls.Add(Me.prgRemoteDatabase, 1, 1)
        Me.TableLayoutPanel1.Controls.Add(Me.lblLocalDatabaseStatus, 2, 0)
        Me.TableLayoutPanel1.Controls.Add(Me.lblRemoteDatabaseStatus, 2, 1)
        Me.TableLayoutPanel1.Controls.Add(Me.Label4, 0, 0)
        Me.TableLayoutPanel1.Controls.Add(Me.Label5, 0, 1)
        Me.TableLayoutPanel1.Controls.Add(Me.prgAmazon, 1, 2)
        Me.TableLayoutPanel1.Controls.Add(Me.Label6, 0, 2)
        Me.TableLayoutPanel1.Controls.Add(Me.lblAmazonStatus, 2, 2)
        Me.TableLayoutPanel1.Controls.Add(Me.prgEbay, 1, 3)
        Me.TableLayoutPanel1.Controls.Add(Me.lblEbayStatus, 2, 3)
        Me.TableLayoutPanel1.Controls.Add(Me.Label1, 0, 3)
        Me.TableLayoutPanel1.Location = New System.Drawing.Point(8, 7)
        Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
        Me.TableLayoutPanel1.RowCount = 4
        Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle())
        Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle())
        Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle())
        Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle())
        Me.TableLayoutPanel1.Size = New System.Drawing.Size(425, 107)
        Me.TableLayoutPanel1.TabIndex = 8
        '
        'prgLocalDatabase
        '
        Me.prgLocalDatabase.Location = New System.Drawing.Point(110, 3)
        Me.prgLocalDatabase.Name = "prgLocalDatabase"
        Me.prgLocalDatabase.Size = New System.Drawing.Size(244, 20)
        Me.prgLocalDatabase.TabIndex = 0
        '
        'prgRemoteDatabase
        '
        Me.prgRemoteDatabase.Location = New System.Drawing.Point(110, 29)
        Me.prgRemoteDatabase.Name = "prgRemoteDatabase"
        Me.prgRemoteDatabase.Size = New System.Drawing.Size(244, 20)
        Me.prgRemoteDatabase.TabIndex = 1
        '
        'lblLocalDatabaseStatus
        '
        Me.lblLocalDatabaseStatus.Anchor = System.Windows.Forms.AnchorStyles.Left
        Me.lblLocalDatabaseStatus.AutoSize = True
        Me.lblLocalDatabaseStatus.Location = New System.Drawing.Point(360, 6)
        Me.lblLocalDatabaseStatus.MinimumSize = New System.Drawing.Size(40, 13)
        Me.lblLocalDatabaseStatus.Name = "lblLocalDatabaseStatus"
        Me.lblLocalDatabaseStatus.Size = New System.Drawing.Size(40, 13)
        Me.lblLocalDatabaseStatus.TabIndex = 3
        '
        'lblRemoteDatabaseStatus
        '
        Me.lblRemoteDatabaseStatus.Anchor = System.Windows.Forms.AnchorStyles.Left
        Me.lblRemoteDatabaseStatus.AutoSize = True
        Me.lblRemoteDatabaseStatus.Location = New System.Drawing.Point(360, 32)
        Me.lblRemoteDatabaseStatus.MinimumSize = New System.Drawing.Size(40, 13)
        Me.lblRemoteDatabaseStatus.Name = "lblRemoteDatabaseStatus"
        Me.lblRemoteDatabaseStatus.Size = New System.Drawing.Size(40, 13)
        Me.lblRemoteDatabaseStatus.TabIndex = 4
        '
        'Label4
        '
        Me.Label4.Anchor = System.Windows.Forms.AnchorStyles.Right
        Me.Label4.AutoSize = True
        Me.Label4.Location = New System.Drawing.Point(4, 6)
        Me.Label4.MinimumSize = New System.Drawing.Size(100, 13)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(100, 13)
        Me.Label4.TabIndex = 6
        Me.Label4.Text = "Local Database:"
        Me.Label4.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'Label5
        '
        Me.Label5.Anchor = System.Windows.Forms.AnchorStyles.Right
        Me.Label5.AutoSize = True
        Me.Label5.Location = New System.Drawing.Point(4, 32)
        Me.Label5.MinimumSize = New System.Drawing.Size(100, 13)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(100, 13)
        Me.Label5.TabIndex = 7
        Me.Label5.Text = "Remote Database:"
        Me.Label5.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'prgAmazon
        '
        Me.prgAmazon.Location = New System.Drawing.Point(110, 55)
        Me.prgAmazon.Name = "prgAmazon"
        Me.prgAmazon.Size = New System.Drawing.Size(244, 20)
        Me.prgAmazon.TabIndex = 9
        '
        'Label6
        '
        Me.Label6.Anchor = System.Windows.Forms.AnchorStyles.Right
        Me.Label6.AutoSize = True
        Me.Label6.Location = New System.Drawing.Point(4, 58)
        Me.Label6.MinimumSize = New System.Drawing.Size(100, 13)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(100, 13)
        Me.Label6.TabIndex = 8
        Me.Label6.Text = "Amazon:"
        Me.Label6.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'lblAmazonStatus
        '
        Me.lblAmazonStatus.Anchor = System.Windows.Forms.AnchorStyles.Left
        Me.lblAmazonStatus.AutoSize = True
        Me.lblAmazonStatus.Location = New System.Drawing.Point(360, 58)
        Me.lblAmazonStatus.MinimumSize = New System.Drawing.Size(40, 13)
        Me.lblAmazonStatus.Name = "lblAmazonStatus"
        Me.lblAmazonStatus.Size = New System.Drawing.Size(40, 13)
        Me.lblAmazonStatus.TabIndex = 5
        '
        'prgEbay
        '
        Me.prgEbay.Location = New System.Drawing.Point(110, 81)
        Me.prgEbay.Name = "prgEbay"
        Me.prgEbay.Size = New System.Drawing.Size(244, 20)
        Me.prgEbay.TabIndex = 11
        '
        'lblEbayStatus
        '
        Me.lblEbayStatus.Anchor = System.Windows.Forms.AnchorStyles.Left
        Me.lblEbayStatus.AutoSize = True
        Me.lblEbayStatus.Location = New System.Drawing.Point(360, 86)
        Me.lblEbayStatus.MinimumSize = New System.Drawing.Size(40, 13)
        Me.lblEbayStatus.Name = "lblEbayStatus"
        Me.lblEbayStatus.Size = New System.Drawing.Size(40, 13)
        Me.lblEbayStatus.TabIndex = 12
        '
        'Label1
        '
        Me.Label1.Anchor = System.Windows.Forms.AnchorStyles.Right
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(4, 86)
        Me.Label1.MinimumSize = New System.Drawing.Size(100, 13)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(100, 13)
        Me.Label1.TabIndex = 10
        Me.Label1.Text = "Ebay:"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'lbStatus
        '
        Me.lbStatus.FormattingEnabled = True
        Me.lbStatus.Location = New System.Drawing.Point(7, 120)
        Me.lbStatus.Name = "lbStatus"
        Me.lbStatus.Size = New System.Drawing.Size(425, 56)
        Me.lbStatus.TabIndex = 9
        '
        'ctrlTestStatus
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.Controls.Add(Me.TableLayoutPanel1)
        Me.Controls.Add(Me.lbStatus)
        Me.Name = "ctrlTestStatus"
        Me.Size = New System.Drawing.Size(440, 185)
        Me.TableLayoutPanel1.ResumeLayout(False)
        Me.TableLayoutPanel1.PerformLayout()
        Me.ResumeLayout(False)

    End Sub

    Friend WithEvents TableLayoutPanel1 As TableLayoutPanel
    Friend WithEvents prgLocalDatabase As ProgressBar
    Friend WithEvents prgRemoteDatabase As ProgressBar
    Friend WithEvents lblLocalDatabaseStatus As Label
    Friend WithEvents lblRemoteDatabaseStatus As Label
    Friend WithEvents Label4 As Label
    Friend WithEvents Label5 As Label
    Friend WithEvents prgAmazon As ProgressBar
    Friend WithEvents Label6 As Label
    Friend WithEvents lblAmazonStatus As Label
    Friend WithEvents prgEbay As ProgressBar
    Friend WithEvents lblEbayStatus As Label
    Friend WithEvents Label1 As Label
    Friend WithEvents lbStatus As ListBox
End Class

以及我将控件放到上面的表单的设计器代码(在我放入之前):

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class frmTestStart
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()>
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()>
    Private Sub InitializeComponent()
        Me.SuspendLayout()
        '
        'frmTestStart
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(468, 206)
        Me.Name = "frmTestStart"
        Me.Text = "Test Connections"
        Me.ResumeLayout(False)

    End Sub

End Class

我拖动控件保存后设计器生成的内容:

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class frmTestStart
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()>
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()>
    Private Sub InitializeComponent()
        Me.CtrlTestStatus1 = New QBI.ctrlTestStatus()
        Me.SuspendLayout()
        '
        'CtrlTestStatus1
        '
        Me.CtrlTestStatus1.Location = New System.Drawing.Point(13, 13)
        Me.CtrlTestStatus1.Name = "CtrlTestStatus1"
        Me.CtrlTestStatus1.Size = New System.Drawing.Size(440, 185)
        Me.CtrlTestStatus1.TabIndex = 0
        '
        'frmTestStart
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(468, 206)
        Me.Controls.Add(Me.CtrlTestStatus1)
        Me.Name = "frmTestStart"
        Me.Text = "Test Connections"
        Me.ResumeLayout(False)

    End Sub

    Friend WithEvents CtrlTestStatus1 As QBI.ctrlTestStatus
End Class

我在 QBI.ctrlTestStatus() 下得到了波浪线,如果我尝试编译它会生成错误。我还以为是设计器的命名空间bug,所以立马把usercontrol包起来修复(不高兴)。项目命名空间是QBI(据我所知不是保留字)。

Namespace QBI
    Public Class ctrlTestStatus
        Private Sub ctrlTestStatus_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        End Sub
    End Class

End Namespace

不幸的是,这造成了更多问题,并且 CtrlTestStatus1 实例的属性和事件不可用(我得到了波浪线)。

由于 64 位/32 位问题(VS 仍然是 32 位!),我从 MS 中发现了一个问题,将编译设置为任何 CPU(项目构建一直设置为此),但也没有任何乐趣。

关于如何解决这个问题的任何想法(设置、我做错的事情)?

【问题讨论】:

  • 我删除了 QBI。来自 QBI.ctrlTestStatus 并且有效,但是,对控件的任何更改都将覆盖我所做的代码更改(将来)。这现在可以工作,因为我已经无计可施了,只需要从这里开始手动更改代码 - MS 需要修复这个错误(从我在其他帖子中看到的 VS2008 以来它一直存在 - 那,也没有有效的解决方案)
  • 从头开始!它没有用!啊啊啊啊啊!!!

标签: vb.net winforms visual-studio-2015 user-controls


【解决方案1】:

旧帖子,但对于遇到此问题的其他人...

我在一个项目中发生了这种情况,我定义了一个与我的项目(根命名空间)同名的子命名空间(在 OP 的情况下,“QBI”)。当您的项目中某处的代码看起来像这样时,就会发生这种情况:

Namespace [Name of your Project/Root Namespace] [...] End Namespace

这样做的结果是您的项目包含“QBI”命名空间和“QBI.QBI”命名空间(如果您查看项目的对象浏览器,您可以自己看到)。这种情况会导致设计器文件中出现分辨率冲突,从而导致它在您的自定义控件的错误命名空间下查找。

解决此问题的几种可能方法:

  1. 在声明子命名空间的地方(如上面的示例代码),完全删除命名空间声明。这会将声明中的所有代码移动到您的根“QBI”命名空间。如果不需要在子命名空间中包含该代码,您可以这样做。
  2. 在声明子命名空间的地方,右键单击命名空间名称并将其重命名为您的根命名空间名称以外的名称。
  3. 如果您想保留子命名空间的名称,您可以改为将根命名空间重命名为唯一的名称,从我的项目 --> 应用程序 --> 根命名空间。

选项 1 是最简单的选项,应该可以立即解决错误。

选项 2 是下一个最简单的方法,但是,例如,如果您将子命名空间重命名为“NewName”,则 Designer 文件中对“ctrlTestStatus”的所有引用都会错误地重命名为“NewName.ctrlTestStatus”,并且您'必须将它们修复为“QBI.ctrlTestStatus”。

选项 3 是最繁琐的,因为您必须使用查找和替换来解析对根命名空间的所有 Designer 文件引用,注意不要错误地替换对子 QBI 命名空间的合法引用。

【讨论】:

    猜你喜欢
    • 2016-06-30
    • 2015-08-05
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    相关资源
    最近更新 更多