在VB.NET中禁用表单的默认实例 [英] Disable default instance of forms in VB.NET

查看:99
本文介绍了在VB.NET中禁用表单的默认实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循这个答案.我可以禁用自动实例化吗?我希望VS显示错误:

Following this answer. Can I disable automatic instantiation? I want VS to show the error:

引用非共享成员需要对象引用

Reference to a non-shared member requires an object reference

推荐答案

根据创建项目时选择的初始项目模板,初始化Visual Studio中启用的功能.通常,将选择"Windows Forms App(.Net Framework)Visual Basic"模板,如下所示.

The features enabled in Visual Studio are initialized based on the initial project template you select when creating a project. Normally, one would select the "Windows Forms App(.Net Framework) Visual Basic" template as shown below.

(注意:所有图像均基于VS 2017版本15.8.7)

这将加载一个VB项目环境,其中包括大量自动代码生成,这些代码通过添加My Namespace来支持VB应用程序框架,其中包括默认表单实例" 功能(请参见: My.Internals:检查Visual Basic我的功能.现在您可以尝试通过项目属性"->应用选项卡"->启用应用程序框架"复选框来禁用此框架,但所有要做的就是要求您提供自己的"Sub Main"实现.

This will load up a VB project environment that includes a lot of automatic code generation that supports the VB Application Framework including the "Default Form Instance" feature via additions to the My Namespace (see: My.Internals: Examining the Visual Basic My Feature. Now you could try to disable this framework via the Project Properties->Appliction Tab->Enable Application Framework checkbox, but all that does is require you to provide you own "Sub Main" implementation.

要创建一个不包含所有我的命名空间"代码生成(包括默认表单实例)的项目,您需要从空项目(.Net Framework)"模板开始.

To create a project that free of all the My Namespace code generation including the the default form instances, you need to start with the "Empty Project (.Net Framework)" template.

这将创建一个准VB项目.如果添加WinForm(项目"菜单->添加窗口表单")并转到代码视图并添加以下内容:

This will create a bare-bones VB project. If you add a WinForm (Project Menu->Add Window Form) and go to the code-view and add the following:

Class Demo_NoDefaultForms
    Sub ErrorOnForm1Reference()
        Form1.Text = "ain't gonna work"
    End Sub
End Class

您将看到不支持默认的Form1实例.

You will see that the default Form1 instance is not supported.

此外,项目属性中的所有应用程序框架"内容均已禁用.

Furthermore, all the "Application Framework" stuff in the project properties is disabled.

项目类型My Namespace信息就像所有项目信息都存储在 projName.vbproj 文件中,并存储在<MyType>标记中.尽管我从未在此标签上找到任何官方文档,但以下内容总结了我对创建各种项目类型的观察.

The project type My Namespace information is like all project information is stored in the projName.vbproj file and is stored in the <MyType> tag. Although I have never found any official documentation on this tag, the following summarizes my observations from creating various project types.


MyType Tag Value                  Project Type
-----------------------------     ------------------------------------------------
WindowsForms                      Normal Windows Form App w/ Application Framework enabled
WindowsFormsWithCustomSubMain     Normal Windows Form App w/ Application Framework disabled
Console                           Console App w/ Application Framework enabled
Custom                            WPF Application
Empty                             No My Namespace additions

从"Empty Project(.Net Framework)"模板开始将很乏味,因为您需要执行大量样板设置并每次都声明一个"Sub Main".我建议您使用所有自定义项创建一个基础项目,然后将该项目导出一个新模板(请参阅:

Starting with the "Empty Project (.Net Framework)" template would be tedious as you would need to perform a lot of boiler-plate setup and declare a "Sub Main" each time. I recommend that you create a base project with all your own customizations and then export the project a new template (see: How to: Create project templates.

这篇关于在VB.NET中禁用表单的默认实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆