如何在 .NET 中创建和使用资源
如何创建一个可以在程序的各个部分轻松引用和使用的资源?
我的具体问题是我有一个 NotifyIcon,我想根据程序的状态更改其图标。 这是一个常见问题,但我已经困扰了很长时间。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,在四处搜索并拼凑了 StackOverflow 周围的各个点之后(哎呀,我已经喜欢这个地方了),大多数问题已经过了这个阶段。 不过,我确实设法找到了问题的答案。
如何创建资源:
就我而言,我想创建一个图标。 无论您想要添加什么类型的数据作为资源,这都是一个类似的过程。
如何使用资源:
太好了,我们有了新资源,我们渴望拥有那些可爱的变化图标...我们该怎么做呢? 幸运的是,C# 使这一切变得非常简单。
有一个名为
Properties.Resources
的静态类,它使您可以访问所有资源,因此我的代码最终非常简单:完成! 完成的! 当你知道如何做时,一切都会变得简单,不是吗?
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.
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.
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.
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:Done! Finished! Everything is simple when you know how, isn't it?
上面的内容实际上并不适合我,正如我对 Visual Studio 2010 所期望的那样。它不允许我访问 Properties.Resources,说由于权限问题无法访问。 我最终不得不更改资源属性中的持久性设置,然后我找到了如何通过 Resources.Designer.cs 文件访问它,其中它有一个自动 getter,让我可以通过 MyNamespace.Properties.Resources 访问图标.NameFromAddingTheResource。 它返回一个 Icon 类型的对象,可供使用。
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.
上面的方法效果很好。
另一种方法(我假设这里是网络)是创建您的页面。 向页面添加控件。 然后在设计模式下转到:工具> 生成本地资源。 解决方案中将自动出现一个资源文件,页面中的所有控件都映射到资源文件中。
要创建其他语言的资源,请将 4 个字符的语言附加到文件名末尾、扩展名之前(Account.aspx.en-US.resx、Account.aspx.es-ES.resx ...ETC)。
要检索代码隐藏中的特定条目,只需调用此方法:
GetLocalResourceObject([资源条目键/名称])
。The above method works well.
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.
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).
To retrieve specific entries in the code-behind, simply call this method:
GetLocalResourceObject([resource entry key/name])
.Matthew Scharley 发布的代码存在内存泄漏:
您应该在替换之前
Dispose()
notifyIcon.Icon,因为Properties.Resources.SOME_ICON
创建了一个新的Icon< /code> 每次使用时。
这可以通过以下代码在日志中观察到:
您将在日志中看到 3 个不同的哈希代码。 这意味着这些是不同的对象。
因此,简单的修复方法是:
Code posted by Matthew Scharley has a memory leak:
You should
Dispose()
notifyIcon.Icon before replacing it, becauseProperties.Resources.SOME_ICON
creates a newIcon
each time it is used.This can be observed in the log, with this code:
You will see 3 different Hash Codes in the log. This means these are different Objects.
So, the simple fix will be: