C# 如何在 .NET 中创建和使用资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/90697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create and use resources in .NET
提问by Matthew Scharley
How do I create a resource that I can reference and use in various parts of my program easily?
如何创建可以在程序的各个部分轻松引用和使用的资源?
My specific problem is that I have a NotifyIcon that I want to change the icon of depending on the state of the program. A common problem, but one I've been struggling with for a long time.
我的具体问题是我有一个 NotifyIcon,我想根据程序的状态更改图标。一个常见的问题,但我一直在努力解决一个问题。
采纳答案by Matthew Scharley
Well, after searching around and cobbling together various points from around StackOverflow (gee, I love this place already), most of the problems were already past this stage. I did manage to work out an answer to my problem though.
好吧,在四处寻找并拼凑 StackOverflow 周围的各个点之后(哎呀,我已经喜欢这个地方了),大多数问题已经过了这个阶段。不过,我确实设法解决了我的问题。
How to create a resource:
如何创建资源:
In my case, I want to create an icon. It's a similar process, no matter what type of data you want to add as a resource though.
就我而言,我想创建一个图标。这是一个类似的过程,不管你想添加什么类型的数据作为资源。
- Right click the project you want to add a resource to. Do this in the Solution Explorer. Select the "Properties" option from the list.
- Click the "Resources" tab.
- The first button along the top of the bar will let you select the type of resource you want to add. It should start on string. We want to add an icon, so click on it and select "Icons" from the list of options.
- Next, move to the second button, "Add Resource". You can either add a new resource, or if you already have an icon already made, you can add that too. Follow the prompts for whichever option you choose.
- At this point, you can double click the newly added resource to edit it. Note, resources also show up in the Solution Explorer, and double clicking there is just as effective.
- 右键单击要向其添加资源的项目。在解决方案资源管理器中执行此操作。从列表中选择“属性”选项。
- 单击“资源”选项卡。
- 栏顶部的第一个按钮可让您选择要添加的资源类型。它应该从字符串开始。我们要添加一个图标,因此单击它并从选项列表中选择“图标”。
- 接下来,移至第二个按钮“添加资源”。您可以添加新资源,或者如果您已经制作了一个图标,您也可以添加它。按照您选择的任何选项的提示进行操作。
- 此时,您可以双击新添加的资源进行编辑。请注意,资源也会显示在解决方案资源管理器中,双击也同样有效。
How to use a resource:
如何使用资源:
Great, so we have our new resource and we're itching to have those lovely changing icons... How do we do that? Well, lucky us, C# makes this exceedingly easy.
太好了,所以我们有了我们的新资源,我们渴望拥有那些可爱的不断变化的图标......我们如何做到这一点?好吧,幸运的是,C# 使这变得非常容易。
There is a static class called Properties.Resources
that gives you access to all your resources, so my code ended up being as simple as:
有一个静态类Properties.Resources
可以让您访问所有资源,因此我的代码最终变得如此简单:
paused = !paused;
if (paused)
notifyIcon.Icon = Properties.Resources.RedIcon;
else
notifyIcon.Icon = Properties.Resources.GreenIcon;
Done! Finished! Everything is simple when you know how, isn't it?
完毕!完成的!当你知道怎么做时,一切都很简单,不是吗?
回答by Chuck Conway
The above method works good.
上述方法效果很好。
Another method (I am assuming web here) is to create your page. Add controls to the page. Then while in design mode go to: Tools > Generate Local Resource. A resource file will automatically appear in the solution with all the controls in the page mapped in the resource file.
另一种方法(我在这里假设是网络)是创建您的页面。向页面添加控件。然后在设计模式下转到:Tools > Generate Local Resource。资源文件将自动出现在解决方案中,页面中的所有控件都映射到资源文件中。
To create resources for other languages, append the 4 character language to the end of the file name, before the extension (Account.aspx.en-US.resx, Account.aspx.es-ES.resx...etc).
要为其他语言创建资源,请将 4 个字符的语言附加到文件名的末尾,在扩展名之前(Account.aspx.en-US.resx、Account.aspx.es-ES.resx...等)。
To retrieve specific entries in the code-behind, simply call this method: GetLocalResourceObject([resource entry key/name])
.
要检索代码隐藏特定条目,只需调用这个方法:GetLocalResourceObject([resource entry key/name])
。
回答by Quinxy von Besiex
The above didn't actually work for me as I had expected with Visual Studio 2010. It wouldn't let me access Properties.Resources, said it was inaccessible due to permission issues. I ultimately had to change the Persistence settings in the properties of the resource and then I found how to access it via the Resources.Designer.cs file, where it had an automatic getter that let me access the icon, via MyNamespace.Properties.Resources.NameFromAddingTheResource. That returns an object of type Icon, ready to just use.
以上实际上并没有像我对 Visual Studio 2010 所期望的那样对我有用。它不会让我访问 Properties.Resources,说由于权限问题而无法访问。我最终不得不更改资源属性中的 Persistence 设置,然后我找到了如何通过 Resources.Designer.cs 文件访问它,它有一个自动获取器,让我可以通过 MyNamespace.Properties.Resources 访问图标.NameFromAddingTheResource。这将返回一个 Icon 类型的对象,随时可以使用。