如何创建和在.NET中使用的资源 [英] How to create and use resources in .NET

查看:96
本文介绍了如何创建和在.NET中使用的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何创建我可以引用和容易我的程序的不同部分使用的资源?

How do I create a resource that I can reference and use in various parts of my program easily?

我的具体问题是,我有我想改变这取决于程序的状态图标将NotifyIcon。一个常见的​​问题,而是一个我一直在挣扎了很长的时间。

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.

推荐答案

好了,从各地计算器搜索周围,拼凑不同点后(哎呀,我喜欢这个地方的话),大部分问题已经过去的这个阶段。我没有管理工作,虽然出来回答我的问题。

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.

如何创建资源。

在我的情况,我想创建一个图标。这是一个类似的过程,不管你要添加作为一种资源,虽然什么样的数据。

In my case, I want to create an icon. It's a similar process, no matter what sort of data you want to add as a resource though.


  • 右键单击要添加资源到项目中。在Solution Explorer中执行此操作。从列表中选择属性选项。

  • 单击资源选项卡。

  • 沿杆顶部的第一个按钮可以让你选择你要添加的资源类型。它应该开始字符串。我们要添加一个图标,所以点击它,然后从选项列表中选择图标。

  • 接下来,移动到第二个按钮,添加资源。您可以添加新资源,或者如果你已经有一个图标已经作出,您可以添加太多。请按照您选择任何选项的提示。

  • 在这一点上,你可以双击新添加的资源进行编辑。请注意,资源在解决方案资源管理器中也显示了,双击有同样有效

如何使用资源。

大,所以我们有我们新的资源,我们渴望拥有那些可爱的改变图标...我们该怎么做呢?好了,我们很幸运,C#使得这个非常容易的。

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.

Properties.Resources 称为静态类,您可以访问所有的资源,所以我的code结束了简单的:

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:

paused = !paused;
if (paused)
    notifyIcon.Icon = Properties.Resources.RedIcon;
else
    notifyIcon.Icon = Properties.Resources.GreenIcon;

完成!菲尼斯!一切都很简单,当你知道怎么了,不是吗?

Done! Finis! Everything is simple when you know how, no?

这篇关于如何创建和在.NET中使用的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆