涕淌居

牛~冬~@~瘋~牛~涕~淌~の~涕~淌~居~~GnuDoyng's Web Log~~~Contact me~niu_dong@msn.com~ICQ:277276843~QQ:123930135~

原创 [译]Visual Basic 2005在语言上的增强(二)My收藏

从Visual Basic 6.0甚至是更早版本迁移到.NET平台下的开发人员所面临的一个主要挑战就是.NET框架本身庞大的规模及其命名空间层次中繁多的类。经常使用的应用程序以及环境变量都可以在.NET框架下的BCL(Base Class Library,涕淌注。)中找到,不过能熟练地定位它们是需要时间的。Visual Basic 2005中的这个新的My层次为许多频繁使用的框架库提供了便捷的、组织有条理的访问接口,从而加速并简化了应用程序的开发。

My有两条令人称奇的特性:
    * 附属条项能够提供组织有序的访问框架库的快捷方式。
    * 一系列的动态对象能够在你向你的项目中添加窗体、设置、资源,以及Web服务时被创建。

My快捷方式在本质上是访问那些经常为人所用的BCL对象的捷径。最顶级的My快捷方式有My.Application、My.Computer,还有My.User,其中的每一个又包含了它自己的对象层次。例如,My.User对象暴露了AudioFileSystemKeyboardMouseNetworkRegistry等等:

Dim sVar As String
sVar = My.User.Identity.Name
sVar = My.Computer.Info.OSFullName
sVar = My.Application.CommandLineArgs.Count.ToString

当你往你的项目中添加窗体、资源、设置,以及Web服务时,Visual Basic能自动为你动态地填充My对象。My.Resources和My.Settings尤其地有趣,因为在你往项目中添加资源和设置的时候,Visual Basic编译器能生成强类型的变量引用。

在Solution Explorer中双击My Project项来显示应用程序编辑器。如图2所示,在表项的左栏中点击Settings

图2 展示了设置表格的应用程序编辑器。错误列表面板用于显示错误、警告,以及其他信息,并且已经和任务列表分离开了。

添加一个用户作用域的Integer型的设置名MaxInputValue,并给它赋值100。这样你就可以马上在你的代码里通过智能感知来使一个强类型变量引用这个设置,就像图3所示的那样。

图3 一个对你在设置应用程序编辑器里创建的设置值进行智能感知的例子。

在设置编辑器的右上角,有一个标注了View Code的链接。点击这个链接,可以查看到一个名为MySetting_user.vb。这个文件是一个部分类,你可以添加代码处理设置事件,这样当你的设置改变时,你的应用程序的状态也随之更新(参见部分类型以获得更多内容)。

Partial Public Class MySettings
    '...
End Class

MySettings中暴露了三个事件:
 PropertyChanged
 SettingChanging
 SettingSaving

应用程序设置的接口是可填充的,这意味着你可以通过实现这些事件的常规历程来存储你自定义的设置。

你也可以得到你在项目中所添加资源的强类型。如果你把一个名为Happy.bmp的图像拖放到Resources编辑器的Images工作区里,你就可以使用My.Resources.Happy这样的代码来引用它。Happy是Bitmap变量类型,因此,你不必通过铸造一个Object或是Image变量来在代码中使用它。当你在代码里敲入Happy,智能感知同样能迅速地起作用。譬如:

PictureBox1.Image = My.Resources.Happy

此行代码把这个位图变量赋给了一个PictureBox的Image属性。你可以看看在图4中所示的在智能感知弹出的窗口,它告诉你这个对象是Bitmap类型的。

图4 把一个位图图象拖放到编辑应用程序的资源表格里将产生一个强类型变量,你可以立即在你的代码里引用它。

随着你在项目中添加窗体和Web服务,My变量总是不断地动态更新个中的默认强类型实例,在你的代码中都可以引用它们。

正如你所见的,My能使得你更快捷地查找应用程序及其环境与BCL相关的类,并允许你以简便的、强类型式的访问方式来访问设置、资源以及其他你添加在工程里的对象。无论对任何层次的Visual Basic开发人员,这些特性都将极大地提高开发效率。

@以下附上原文供大家参考@

My

A major challenge faced by developers migrating to .NET from Visual Basic 6.0 and earlier versions is the sheer size of the .NET Framework and the number of classes in its namespace hierarchy. Commonly used application and environment objects are found throughout the Base Class Library (BCL) of the .NET Framework, and it takes time to learn their locations. The new My hierarchy in Visual Basic 2005 speeds up and simplifies application development by providing convenient, logically organized access to many commonly used framework classes.

My comes in two flavors:
• Items that provide well-organized shortcuts into the Framework classes.
• A set of dynamic objects created as you add forms, settings, resources, and Web services to your projects.

The My shortcuts are essentially a speed dial into many commonly used BCL objects. The top-level My shortcuts are My.Application, My.Computer, and My.User, each of which contains its own object hierarchy. For example, the My.User object exposes Audio, FileSystem, Keyboard, Mouse, Network, Registry, and more:

Dim sVar As String
sVar = My.User.Identity.Name
sVar = My.Computer.Info.OSFullName
sVar = My.Application.CommandLineArgs.Count.ToString

The dynamic My objects are automatically populated by Visual Basic as you add forms, resources, settings, and Web services to your project. My.Resources and My.Settings are particularly interesting, as the Visual Basic compiler generates strongly typed variable references as you add resources and settings to your project.

Double-click the My Project item in Solution Explorer to bring up the application designer. Click on Settings in the tab list on the left side as demonstrated in Figure 2.

Figure 2. Application Designer showing the Settings tab. The Error List pane shows errors, warnings, and messages and is now separate from the Task List.

Add a setting name such as MaxInputValue of type Integer at User scope with a value of 100. You can immediately refer to this setting with a strongly typed object in your code with full IntelliSense as shown in Figure 3.

Figure 3. Example of the IntelliSense pop-up for a settings value you create in the Settings application designer

At the top right corner of the Settings designer is a link labeled View Code. Click on this link to view the file named MySettings_user.vb. This file is a partial class where you can add code to handle settings events to update the state of your application as your settings change (see the section on Partial Types for more information):

Partial
Public Class MySettings
    '...
End Class

Three MySettings events are exposed here:
    • PropertyChanged
    • SettingChanging
    • SettingsSaving

The application Settings interface is pluggable, meaning that you can define how your settings are saved by implementing these event routines.

You get the same strong typing with Resources you add to your project. If you drop an image named Happy.bmp on the Images area of the Resources designer, you can refer to it in code as My.Resources.Happy. Happy is of type Bitmap, which means that you don't have to cast from an Object or Image type to use the bitmap in code. IntelliSense also works immediately when you type Happy in your code. For example:

PictureBox1.Image = My.Resources.HAPPY

This line of code assigns the bitmap object to a PictureBox's Image property. You can see the IntelliSense pop-up window in Figure 4, indicating that the object is of type Bitmap.

Figure 4. Dragging-and-dropping a bitmap image on the Resources tab in the application designer results in a strongly typed object that you can use immediately in your code.

As you add forms and Web services to your project, the My object is dynamically populated with strongly typed default instances of these objects that you can refer to in your code.

As you can see, My makes it more convenient to find application and environment related BCL classes, and gives you immediate and strongly typed access to settings, resources, and other objects you add to your projects. These are great productivity enhancements for Visual Basic developers of any experience level.

发表于 @ 2004年12月20日 12:12:00|评论(loading...)

新一篇: [译]Visual Basic 2005在语言上的增强(三)XML注释 | 旧一篇: 向自己保證~再也不熬夜寫程序了

用户操作
[即时聊天] [发私信] [加为好友]
疯牛涕淌
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
疯牛涕淌的公告
123930135~有蝦米代誌請Q我哦! 本blog已搬家...
文章分类
收藏
百科全書
Britannica
Encyclopedia Mythica
Encyclopedia of Philosophy
Encyclopedia.com
MSN Encarta
WIKIPEDIA
大師之家
Code/Tea/Etc...
Mind View
Panopticon Central
Robert Green's VB Blog
Rockford Lhotka
The Mountain of Worthless Information
The VB Team
VB.NET Expert
侯捷網站
話語資源
American Dialects
Ethnologue
Glossary
IPA HTML Entities
Lexicon of Linguistics
The Linguistic Society of America
Übungen Für Deutsch
Word 2 Word
WordNet
Yamada Language Guides
yourDictionary
技術社區
.NET Framework Windows Forms
ASP.NET Heaven
ASP.NET Web
C# Corner
C# Friends
C# Help
Code Guru
Dev Guru
Developer Fusion
DotNet Zone
Got DotNet
MIT開放式課程網頁
Most Valuable Professional
MSDN
SuperSite for Windows
The Code Project
VB Forums
VB Info Zine
VB.NET Heaven
VBCity
中國 Dot Net Nuke
微軟中國社區
外國朋友
Bram Blogs
Дневник elishena
Добре дошли, приятели!
Заметки обалдевшей от счастья матери
友情鏈接
Amoeba原蟲
kgd jeyp
Let's Eat Noodles at Last
Nature Is Restless
YOYO Forever
三生石坊
去日留痕
咖啡愛我
夢想飛翔
天高任我飛
奔騰不息
宜軒品
寧靜*海豚灣
形而上的憂傷
我愛茉莉
我的生活
既生甲何生乙
明天的寂寞
沫言沫語
異域星空
裝配中的腦袋
閩中龍
阿貴
風一代
與我有關
Linguistics Paradise
UÕH UỎNG SANG
閩東語論壇
只是路過
安妮寶貝
微微微笑
存档
软件项目交易
Csdn Blog version 3.1a
Copyright © 疯牛涕淌