如何在 .NET 中创建和使用资源

发布于 2024-07-06 06:17:10 字数 107 浏览 6 评论 0原文

如何创建一个可以在程序的各个部分轻松引用和使用的资源?

我的具体问题是我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

友谊不毕业 2024-07-13 06:17:10

好吧,在四处搜索并拼凑了 StackOverflow 周围的各个点之后(哎呀,我已经喜欢这个地方了),大多数问题已经过了这个阶段。 不过,我确实设法找到了问题的答案。

如何创建资源

就我而言,我想创建一个图标。 无论您想要添加什么类型的数据作为资源,这都是一个类似的过程。

  • 右键单击要添加资源的项目。 在解决方案资源管理器中执行此操作。 从列表中选择“属性”选项。
  • 单击“资源”选项卡。
  • 栏顶部的第一个按钮可让您选择要添加的资源类型。 它应该从字符串开始。 我们想要添加一个图标,因此单击它并从选项列表中选择“图标”。
  • 接下来,移至第二个按钮“添加资源”。 您可以添加新资源,或者如果您已经制作了图标,也可以添加该资源。 按照提示选择您选择的选项。
  • 此时,您可以双击新添加的资源进行编辑。 请注意,资源也会显示在解决方案资源管理器中,双击那里同样有效。

如何使用资源:

太好了,我们有了新资源,我们渴望拥有那些可爱的变化图标...我们该怎么做呢? 幸运的是,C# 使这一切变得非常简单。

有一个名为 Properties.Resources 的静态类,它使您可以访问所有资源,因此我的代码最终非常简单:

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

完成! 完成的! 当你知道如何做时,一切都会变得简单,不是吗?

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.

  • 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.

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! Finished! Everything is simple when you know how, isn't it?

蓝眼泪 2024-07-13 06:17:10

上面的内容实际上并不适合我,正如我对 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.

本王不退位尔等都是臣 2024-07-13 06:17:10

上面的方法效果很好。

另一种方法(我假设这里是网络)是创建您的页面。 向页面添加控件。 然后在设计模式下转到:工具> 生成本地资源。 解决方案中将自动出现一个资源文件,页面中的所有控件都映射到资源文件中。

要创建其他语言的资源,请将 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]).

定格我的天空 2024-07-13 06:17:10

Matthew Scharley 发布的代码存在内存泄漏:

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

您应该在替换之前Dispose()notifyIcon.Icon,因为Properties.Resources.SOME_ICON创建了一个新的Icon< /code> 每次使用时。
这可以通过以下代码在日志中观察到:

Console.WriteLine(Properties.Resources.RedIcon.GetHashCode());
Console.WriteLine(Properties.Resources.RedIcon.GetHashCode());
Console.WriteLine(Properties.Resources.RedIcon.GetHashCode());

您将在日志中看到 3 个不同的哈希代码。 这意味着这些是不同的对象。

因此,简单的修复方法是:

paused = !paused;
notifyIcon.Icon?.Dispose();
notifyIcon.Icon = paused 
                    ? Properties.Resources.RedIcon;
                    : Properties.Resources.GreenIcon;

Code posted by Matthew Scharley has a memory leak:

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

You should Dispose() notifyIcon.Icon before replacing it, because Properties.Resources.SOME_ICON creates a new Icon each time it is used.
This can be observed in the log, with this code:

Console.WriteLine(Properties.Resources.RedIcon.GetHashCode());
Console.WriteLine(Properties.Resources.RedIcon.GetHashCode());
Console.WriteLine(Properties.Resources.RedIcon.GetHashCode());

You will see 3 different Hash Codes in the log. This means these are different Objects.

So, the simple fix will be:

paused = !paused;
notifyIcon.Icon?.Dispose();
notifyIcon.Icon = paused 
                    ? Properties.Resources.RedIcon;
                    : Properties.Resources.GreenIcon;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文