是否可以在VB.NET WinForms项目的子命名空间中使用表单?

AJ.*_*AJ. 3 vb.net namespaces visual-studio-2010 winforms

如果我在VB.NET中创建一个新的类库项目,我可以创建子文件夹(一个C#),将WinForm对象添加到这些子文件夹,然后指定一个名称空间:

Namespace Sub1.Sub2
    Public Class SomeForm
        Public Sub New()
            InitializeComponent()
        End Sub
    End Class
End Namespace
Run Code Online (Sandbox Code Playgroud)

这解决了ProjectRootNamespace.Sub1.Sub2.SomeForm,这很好.

但是,如果我在VB.NET中创建一个新的WinForms项目,并尝试相同的事情,我在设计器中得到这个错误:

The class SomeForm can be designed, but is not the first class in the file. Visual Studio requires that designers use the first class in the file. Move the class code so that it is the first class in the file and try loading the designer again.

有没有办法在VB.NET WinForms应用程序的子命名空间中而不是在根命名空间中使用表单?

ja7*_*a72 9

您是否在Form.vb和Form.Designer.vb中重命名命名空间?您需要确保两个文件都声明相同的对象.

Form.vb

Namespace X
    Namespace Y
        Public Class Form1

        End Class
    End Namespace
End Namespace
Run Code Online (Sandbox Code Playgroud)

和'Form.Designer.vb`

Namespace X
    Namespace Y
        <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
        Partial Class Form1
            Inherits System.Windows.Forms.Form

            <System.Diagnostics.DebuggerStepThrough()> _
            Private Sub InitializeComponent()
                ...

        End Class
    End Namespace
End Namespace
Run Code Online (Sandbox Code Playgroud)

  • +1,是的.必须单击Solution Explorer窗口中的Show All图标才能找到该文件. (2认同)
  • “命名空间 XY”是表达同一事物的更简洁的方式。 (2认同)