vb,net局部截图_VB.NET中的局部类

vb,net局部截图

Partial Classes are a feature of VB.NET that is used almost everywhere, but there's not much written about it. This might be because there are not a lot of obvious "developer" applications for it yet. The primary use is in the way ASP.NET and VB.NET solutions are created in Visual Studio where it's one of those features that is normally "hidden".

局部类是VB.NET的一项功能,几乎在任何地方都可以使用,但是关于它的文章并不多。 这可能是因为目前还没有很多明显的“开发人员”应用程序。 主要用途是在Visual Studio中创建ASP.NET和VB.NET解决方案的方式,它通常是“隐藏”的功能之一。

A partial class is simply a class definition that is split into more than one physical file. Partial classes don't make a difference to the compiler because all the files that make up a class are simply merged into a single entity for the compiler. Since the classes are just merged together and compiled, you can't mix languages. That is, you can't have one partial class in C# and another in VB. You can't span assemblies with partial classes either. They all have to be in the same assembly.

局部类只是一个类定义,它被拆分为多个物理文件。 局部类对编译器没有影响,因为构成类的所有文件都简单地合并到编译器的单个实体中。 由于这些类只是合并在一起并已编译,因此您不能混合使用语言。 也就是说,在C#中不能有一个局部类,而在VB中不能有另一个。 您也不能使用局部类来跨越程序集。 它们都必须在同一装配中。

This is used a lot by Visual Studio itself, especially in web pages where it is a key concept in "code behind" files. We'll see how this works in a Visual Studio, but understanding what changed in Visual Studio 2005 when it was introduced is a good starting point.

Visual Studio本身经常使用它,特别是在Web页面中,这是“代码隐藏”文件中的关键概念。 我们将看到它在Visual Studio中是如何工作的,但是了解引入Visual Studio 2005时发生的变化是一个很好的起点。

In Visual Studio 2003, the "hidden" code for a Windows application was all in a section called a Region marked "Windows Form Designer generated code". But it was still all there in the same file and it was easy to view, and change, the code in the Region. All of the code is available to your application in .NET. But since some of it is code that you should <almost> never mess with, it was kept in that hidden Region. (Regions can still be used for your own code, but Visual Studio doesn't use them anymore.)

在Visual Studio 2003中,Windows应用程序的“隐藏”代码全部位于名为“ Windows窗体设计器生成的代码”的区域中。 但是,所有内容仍在同一文件中,并且可以轻松查看和更改Region中的代码。 所有代码都可用于.NET中的应用程序。 但是由于其中一些代码是您几乎应该永远不要弄混的代码,因此将其保留在该隐藏区域中。 (区域仍然可以用于您自己的代码,但是Visual Studio不再使用它们。)

In Visual Studio 2005 (Framework 2.0), Microsoft did approximately the same thing, but they hid the code in a different place: a partial class in a separate file. You can see this at the bottom of the illustration below:

在Visual Studio 2005(Framework 2.0)中,Microsoft进行了大致相同的操作,但是将代码隐藏在不同的位置:部分类放在单独的文件中。 您可以在下图的底部看到它:

--------Click Here to display the illustrationClick the Back button on your browser to return--------

--------单击此处以显示插图单击浏览器上的“后退”按钮以返回--------

One of the syntax differences between Visual Basic and C# right now is that C# requires that all partial classes be qualified with the keyword Partial but VB does not. Your main form in VB.NET doesn't have any special qualifiers. But the default class statement for an empty Windows application looks like this using C#:

目前,Visual Basic和C#之间的语法差异之一是C#要求所有部分类都必须使用关键字Partial进行限定,而VB则不需要。 您在VB.NET中的主要形式没有任何特殊的限定词。 但是,使用C#时,空Windows应用程序的默认类语句如下所示:

public partial class Form1 : Form

公共局部类Form1:表单

Microsoft's design choices on things like this are interesting. When Paul Vick, Microsoft's VB designer, wrote about this design choice in his blog Panopticon Central, the debate about it in the comments went on for pages and pages.

微软在诸如此类的事情上的设计选择很有趣。 当Microsoft的VB设计师Paul Vick在他的博客Panopticon Central中写了关于这种设计选择的信息时,有关评论的争论持续不断。

Lets see how all this works with real code on the next page.

让我们在下一页上查看所有这些如何与真实代码一起使用。

On the previous page, the concept of partial classes was explained. We convert a single class into two partial classes on this page.

在上一页中,解释了部分类的概念。 在此页面上,我们将单个类转换为两个部分类。

Here's an example class with one method and one property in a VB.NET project

这是一个示例类,在VB.NET项目中具有一个方法和一个属性

Public Class CombinedClass
   Private m_Property1 As String
   Public Sub New(ByVal Value As String)
      m_Property1 = Value
   End Sub
   Public Sub Method1()
      MessageBox.Show(m_Property1)
   End Sub
   Property Property1() As String
      Get
         Return m_Property1
      End Get
      Set(ByVal value As String)
         m_Property1 = value
      End Set
   End Property
End Class

This class can be called (for example, in the Click event code for a Button object) with the code:

可以使用以下代码调用此类(例如,在Button对象的Click事件代码中):

Dim ClassInstance As New _
   CombinedClass("About Visual Basic Partial Classes")
ClassInstance.Method1()

We can separate the properties and methods of the class into different physical files by adding two new class files to the project. Name the first physical file Partial.methods.vb and name the second one Partial.properties.vb. The physical file names have to be different but the partial class names will be the same so Visual Basic can merge them when the code is compiled.

通过将两个新的类文件添加到项目中,我们可以将类的属性和方法分为不同的物理文件。 将第一个物理文件命名为Partial.methods.vb ,将第二个物理文件命名为Partial.properties.vb 。 物理文件名必须不同,但是部分类名将相同,因此Visual Basic可以在编译代码时合并它们。

It's not a syntax requirement, but most programmers are following the example in Visual Studio of using "dotted" names for these classes. For example, Visual Studio uses the default name Form1.Designer.vb for the partial class for a Windows form. Remember to add the Partial keyword for each class and change the internal class name (not the file name) to the same name. I used the internal class name: PartialClass.

这不是语法要求,但是大多数程序员都遵循Visual Studio中为这些类使用“点分”名称的示例。 例如,Visual Studio使用默认名称Form1.Designer.vb作为Windows窗体的部分类。 请记住为每个类添加Partial关键字,并将内部类名(而不是文件名)更改为相同的名称。 我使用了内部类名称: PartialClass

The illustration below shows all of the code for the example and the code in action.

下图显示了该示例的所有代码以及正在使用的代码。

--------Click Here to display the illustrationClick the Back button on your browser to return--------

--------单击此处以显示插图单击浏览器上的“后退”按钮以返回--------

Visual Studio "hides" partial classes such as Form1.Designer.vb. On the next page, we learn how to do that with the partial classes we just created.

Visual Studio“隐藏”了部分类,例如Form1.Designer.vb。 在下一页上,我们将学习如何使用刚刚创建的局部类来做到这一点。

The previous pages explain the concept of partial classes and show how to code them. But Microsoft uses one more trick with the partial classes generated by Visual Studio. One of the reasons for using them is to separate application logic from UI (user interface) code. In a large project, these two types of code might even be created by different teams. If they're in different files, they can be created and updated with a lot more flexibility. But Microsoft goes one more step and hides the partial code in Solution Explorer as well. Suppose we wanted to hide the methods and properties partial classes in this project? There's a way, but it's not obvious and Microsoft doesn't tell you how.

前几页解释了部分类的概念,并展示了如何对它们进行编码。 但是Microsoft对Visual Studio生成的部分类使用了另一种技巧。 使用它们的原因之一是将应用程序逻辑与UI(用户界面)代码分开。 在大型项目中,这两种类型的代码甚至可能由不同的团队创建。 如果它们位于不同的文件中,则可以更加灵活地创建和更新它们。 但是微软又迈出了一步,并将部分代码也隐藏在解决方案资源管理器中。 假设我们要在该项目中隐藏方法和属性子类? 有一种方法,但是它并不明显,Microsoft不会告诉您如何做。

One of the reasons you don't see the use of partial classes recommended by Microsoft is that it's not really supported very well in Visual Studio yet. To hide the Partial.methods.vb and Partial.properties.vb classes that we just created, for example, requires a change in the vbproj file. This is an XML file that isn't even displayed in Solution Explorer. You can find it with Windows Explorer along with your other files. A vbproj file is shown in the illustration below.

您没有看到Microsoft推荐使用局部类的原因之一是,Visual Studio尚未很好地支持它。 例如,要隐藏刚刚创建的Partial.methods.vb和Partial.properties.vb类,需要对vbproj文件进行更改。 这是一个XML文件, 甚至没有显示在解决方案资源管理器中。 您可以使用Windows资源管理器以及其他文件找到它。 下图显示了一个vbproj文件。

--------Click Here to display the illustrationClick the Back button on your browser to return--------

--------单击此处以显示插图单击浏览器上的“后退”按钮以返回--------

The way we're going to do this is to add a "root" class that is completely empty (only the Class header and End Class statement are left) and make both of our partial classes dependent on it. So add another class named PartialClassRoot.vb and again change the internal name to PartialClass to match the first two. This time, I have not used the Partial keyword just to match the way Visual Studio does it.

我们要执行的方法是添加一个完全为空的“ root”类(仅保留Class标头和End Class语句),并使我们的两个部分类都依赖于此。 因此,添加另一个名为PartialClassRoot.vb的类,然后再次将内部名称更改为PartialClass以匹配前两个名称。 这次,我没有使用Partial关键字只是为了匹配Visual Studio的方式。

Here's where a little knowledge of XML will come in very handy. Since this file will have to be updated manually, you have to get the XML syntax right. You can edit the file in any ASCII text editor - Notepad works just fine - or in an XML editor. It turns out that you have a great one in Visual Studio and that's what is shown in the illustration below. But you can't edit the vbproj file at the same time that you're editing the project it's in. So close the project and open only the vbproj file. You should see the file displayed in the edit window as shown in the illustration below.

在这里,对XML的一点了解将非常有用。 由于必须手动更新此文件,因此您必须正确获取XML语法。 您可以在任何ASCII文本编辑器中编辑文件-记事本就可以正常工作-或在XML编辑器中。 事实证明,您在Visual Studio中拥有出色的能力,这就是下图所示的内容。 但是您不能在编辑项目所在的同时编辑vbproj文件。因此,请关闭该项目并仅打开vbproj文件。 您应该看到编辑窗口中显示的文件,如下图所示。

(Note the Compile elements for each class. DependentUpon sub-elements must be added exactly as shown in the illustration below. This illustration was created in VB 2005 but it has been tested in VB 2008 as well.)

(请注意每个类的Compile元素。DependentUpon子元素必须完全按照下图所示添加。此图创建于VB 2005中,但也已在VB 2008中进行了测试。)

--------Click Here to display the illustrationClick the Back button on your browser to return--------

--------单击此处以显示插图单击浏览器上的“后退”按钮以返回--------

For many of us, it's probably enough to know that partial classes are there, just so we know what they are when we're trying to track down a bug in the future. For large and complex systems development, they could be a small miracle because they can help organize code in ways that would have been impossible before. (You can also have partial structures and partial interfaces!) But some people have concluded that Microsoft invented them just for internal reasons - to make their code generation work better. Author Paul Kimmel even went so far as to suggest that Microsoft actually created partial classes to lower their costs by making it easier to outsource development work around the world.

对于我们中的许多人来说,只要知道部分类就足够了,这样一来,当我们将来尝试查找错误时,我们就知道它们是什么。 对于大型和复杂的系统开发,它们可能是一个小奇迹,因为它们可以帮助以以前不可能的方式组织代码。 (您也可以具有部分结构和部分接口!)但是有些人得出结论,Microsoft出于内部原因发明了它们-使它们的代码生成更好地工作。 作者Paul Kimmel甚至提出微软实际上创建了部分类以通过简化在全球范围内的开发工作来降低其成本。

Maybe. It's the kind of thing they might do.

也许。 他们可能会做这种事情。

翻译自: https://www.thoughtco.com/partial-classes-in-vbnet-3424450

vb,net局部截图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值