vb.net资源文件_什么是VB.Net资源以及如何使用它们?

vb.net资源文件

After Visual Basic students learn all about loops and conditional statements and subroutines, one of the next things that they often ask is, "How do I add a bitmap, a .wav file, a custom cursor, or some other special effect?" One answer is resource files. When you add a resource file to your project, it's integrated for maximum execution speed and minimum hassle when packaging and deploying your application.

在Visual Basic学生学习完有关循环,条件语句和子例程的全部知识之后,他们经常问的下一件事情是:“如何添加位图,.wav文件,自定义光标或某些其他特殊效果?” 答案之一是资源文件。 将资源文件添加到项目中时,将其集成在一起,以在打包和部署应用程序时实现最大的执行速度和最小的麻烦。

Using resource files isn't the only way to include files in a VB project, but it has real advantages. For example, you could include a bitmap in a PictureBox control or use the mciSendString Win32 API. 

使用资源文件不是将文件包含在VB项目中的唯一方法,但是它具有真正的优势。 例如,您可以在PictureBox控件中包含位图,也可以使用mciSendString Win32 API。

Microsoft defines a resource as "any nonexecutable data that is logically deployed with an application."

Microsoft将资源定义为“在逻辑上与应用程序一起部署的任何不可执行的数据”。

The easiest way to manage resource files in your project is to select the Resources tab in the project properties. You bring this up by double-clicking My Project in Solution Explorer or in your project properties under the Project menu item.

在项目中管理资源文件的最简单方法是在项目属性中选择“资源”选项卡。 您可以通过在解决方案资源管理器中或“项目”菜单项下的项目属性中双击“我的项目”来启动此功能。

资源文件的类型 ( Types of Resource Files )

  • Strings

    弦乐
  • Images 

    图片
  • Icons

    图示
  • Audio

    音讯
  • Files

    档案
  • Other

    其他

资源文件简化了全球化 ( Resource Files Simplify Globalization )

Using resource files adds another advantage: better globalization. Resources are normally included in your main assembly, but .NET also lets you package resources into satellite assemblies. This way, you accomplish better globalization because you include only the satellite assemblies that are needed. Microsoft gave each language dialect a code. For example, the American dialect of English is indicated by the string "en-US," and the Swiss dialect of French is indicated by "fr-CH." These codes identify the satellite assemblies that contain culture-specific resource files. When an application runs, Windows automatically uses the resources contained in the satellite assembly with the culture determined from Windows settings.

使用资源文件还有另一个优点:更好的全球化。 资源通常包含在您的主程序集中,但是.NET也允许您将资源打包到附属程序集中。 这样,您可以实现更好的全球化,因为您仅包括所需的附属程序集。 微软为每种语言的方言提供了一个代码。 例如,英语的美国方言用字符串“ en-US”表示,法语的瑞士方言用“ fr-CH”表示。 这些代码标识了包含特定于区域性的资源文件的附属程序集。 运行应用程序时,Windows会根据Windows设置确定的区域性自动使用附属程序集中包含的资源。

VB.Net添加资源文件 ( VB.Net Add Resource Files )

Because resources are a property of the solution in VB.Net, you access them just like other properties: by name using the My.Resources object. To illustrate, examine this application designed to display icons for Aristotle's four elements: air, earth, fire, and water.

因为资源是VB.Net中解决方案的一个属性,所以您可以像访问其他属性一样访问它们:使用My.Resources对象按名称访问。 为了说明这一点,请检查该应用程序 ,该应用程序旨在显示亚里斯多德的四个元素的图标:空气,泥土,火和水。

First, you need to add the icons. Select the Resources tab from your Project Properties. Add icons by choosing Add Existing File from the Add Resources drop-down menu. After a resource is added, the new code looks like this:

首先,您需要添加图标。 从项目属性中选择资源选项卡。 通过从“添加资源”下拉菜单中选择“添加现有文件”来添加图标。 添加资源后,新代码如下所示:


Handles MyBase.Load
处理MyBase.Load
Button1.Image = My.Resources.EARTH.ToBitmap
Button1.Image = My.Resources.EARTH.ToBitmap
Button1.Text = "Earth"
Button1.Text =“地球”
End Sub
结束子

使用Visual Studio嵌入 ( Embedding With Visual Studio )

If you're using Visual Studio, you can embed resources directly into your project assembly. These steps add an image directly to your project:

如果您使用的是Visual Studio,则可以将资源直接嵌入到项目程序集中。 这些步骤将图像直接添加到您的项目:

  • Right-click the project in the Solution Explorer. Click Add and then click Add Existing Item.

    在解决方案资源管理器中右键单击该项目。 单击添加,然后单击添加现有项。
  • Browse to your image file and click Open.

    浏览到您的图像文件,然后单击“打开”。
  • Display the properties for the image that was just added.

    显示刚添加的图像的属性。
  • Set the Build Action property to Embedded Resource.

    将“生成操作”属性设置为“嵌入式资源”。

You can then use the bitmap directly in code like this (where the bitmap was the third one, index number 2 in the assembly).

然后,您可以在这样的代码中直接使用位图(其中位图是第三个位,程序集中的索引号2)。


PictureBox1.Image = New System.Drawing.Bitmap( _
PictureBox1.Image =新System.Drawing.Bitmap(_
GetType(Form1).Assembly.GetManifestResourceStream(res(2)))
GetType(Form1).Assembly.GetManifestResourceStream(res(2)))

Although these resources are embedded as binary data directly in the main assembly or in satellite assembly files, when you build your project in Visual Studio, they're referenced by an XML-based file format that uses the extension .resx. For example, here's a snippet from the .resx file you just created:

尽管这些资源直接作为二进制数据嵌入到主程序集或附属程序集文件中,但是在Visual Studio中构建项目时,它们会被使用扩展名.resx的基于XML的文件格式引用。 例如,这是您刚创建的.resx文件的摘录:


Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
版本= 2.0.0.0,文化=中性,PublicKeyToken = b77a5c561934e089“ />
<data name="AIR"
<data name =“ AIR”
type="System.Resources.ResXFileRef,
type =“ System.Resources.ResXFileRef,
System.Windows.Forms">
System.Windows.Forms“>
<value>..\Resources\CLOUD.ICO;System.Drawing.Icon,
<值> .. \ Resources \ CLOUD.ICO; System.Drawing.Icon,
System.Drawing, Version=2.0.0.0,
System.Drawing,Version = 2.0.0.0,
Culture=neutral,
文化=中立,
PublicKeyToken=b03f5f7f11d50a3a</value>
PublicKeyToken = b03f5f7f11d50a3a </ value>
</data>
</ data>

Because they are just text XML files, a .resx file can't be used directly by a .NET framework application. It has to be converted to a binary ".resources" file, adding it to your application. This job is accomplished by a utility program named Resgen.exe. You might want to do this to create the satellite assemblies for globalization. You have to run resgen.exe from a command prompt.

因为它们只是文本XML文件,所以.NET框架应用程序不能直接使用.resx文件。 必须将其转换为二进制“ .resources”文件,然后将其添加到您的应用程序中。 这项工作由名为Resgen.exe的实用程序完成。 您可能想要执行此操作以创建用于全球化的附属程序集。 您必须从命令提示符下运行resgen.exe。

资源 ( Source )

"Resources Overview." Microsoft, 2015.

“资源概述”。 微软,2015年。

翻译自: https://www.thoughtco.com/vbnet-resource-files-3424443

vb.net资源文件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值