无法通过资源文件更改notifyIcon上的Icon属性 [英] Unable to change Icon Property on notifyIcon via a Resource File

查看:143
本文介绍了无法通过资源文件更改notifyIcon上的Icon属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SharpDevelop 3.2.1编写一个非常简单的VB程序,该程序通过使用notifyIcon组件和contextMenuStrip组件在System Tray中运行。我正在使用 VisualBasicTutorials [ ^ ]作为我的代码的基础。



什么我需要做的是能够根据当前活动的子程序更改系统托盘中显示的图标。



我有一个名为Resouces.resx的资源文件,我已添加了三个图标。



很遗憾,我无法发现如何引用此资源文件以便能够访问图标。 此页 [ ^ ]在msdn声明您所要做的就是用以下方法更改图标属性:



I am using SharpDevelop 3.2.1 to write a very simple VB program that runs in the System Tray by utilizing a notifyIcon component and a contextMenuStrip component. I am using a tutorial found at VisualBasicTutorials[^]as the basis for my code.

What I need to do is be able to change the icon shown in the system tray depending upon what subroutine is currently active.

I have a resource file called Resouces.resx to which I have added three icons.

Unfortunately, I cannot discover how to reference this resource file to be able to access the icons. This page[^] at msdn states that all you have to do is change the icon property with this:

NotifyIcon1.Icon = My.Resouces.YourIcon





如果我输入,一旦我到达'我的'。唯一的选择是应用程序,计算机,表格,用户



如果我忽略这一点并输入行在MSDN上建议,我得到一个编译错误,声明资源不是我的成员



我必须忽略一些东西,但是什么??



If I type this, as soon as I get to 'My.' The only options that come up are Application, Computer, Forms, User

If I ignore this and type in the line as suggested at MSDN, I get a compile error stating that Resources is not a member of My

I must be overlooking something, but what??

推荐答案

你看错了方向。这不是.resx资源的设计方式。您可以在项目结构的任何级别添加资源,并且命名空间也会不同,因为代码是根据基于应用程序的基本命名空间和此位置构建的命名空间名称自动生成的。



因此,获取资源太简单了。首先要了解为您生成静态属性。创建资源文件,为其添加图标。到目前为止,如果在单独的文件中有一个图标,而不是将其嵌入资源中则是有益的。即使您在资源中对其进行了编辑,也请将其另存为文件并从资源中删除。使用添加现有文件添加它,例如YourIcon.ico。确保原始图标不是项目的一部分,以避免重复。添加步骤将:1)将文件复制到项目的某个目录(这就是为什么你必须检查重复并避免它们),2)将文件引用添加到项目文件(因此项目结构,将显示它的一个节点),3)将文件的引用添加到资源文件,4)生成在自动生成的文件中获取此资源的代码,该文件在项目的结构中显示为子节点资源节点。



最后一项是关键项目。打开此自动生成的文件并查看。你会发现一个静态属性,其名称类似于文件的名称,在这种情况下, YourIcon 具有适当的类型,例如图像图标。只需在您的代码中直接使用此属性,使用其完全限定名称(您可以将使用声明添加到您的文件中,也许 alias



如果Visual Studio不知道文件的类型,则自动生成的属性类型将为 byte [] ;使用它的一种方法是将字节作为一个内存流,从这个流中读取对象的实例。



我描述了你的一般方法需要在你的情况下使用。对于我的,这是更具体的。如果你需要了解它是什么,请阅读:

http://msdn.microsoft.com/en-us/library/bb531245.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/8ffec36z.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/6wkcc526.aspx [ ^ ]。



-SA
You are looking in wrong direction. This is not how .resx resources are designed. You can add resources at any level of you project structure, and the namespace will be different, because the code is automatically generated based with the namespace name built based on the base namespace of the application and this location.

So, getting a resource is way too simple. First thing to understand that a static property is generated for you. Create a resource file, add you icon to it. If is, by far, beneficial to have an icon in a separate file, not embed it in a resource. Even if you edited it in a resource, save it as a file and remove from resource. Add it using "Add existing file", say, "YourIcon.ico". Make sure the original icon is not a part of your project, to avoid duplicates. The add step will: 1) copy the file to some directory of your project (that's why you'll have to check for duplicates and avoid them), 2) add the file reference to the project file (and hence project structure, will show a node for it), 3) add a reference to the file to the resource file, 4) generate the code getting this this resource in a auto-generated file, which is shown in a structure of the project as a child node of the resource node.

The last item is the key one. Open this auto-generated file and take a look. You will find a static property with the name similar to the name of the file, in this case, YourIcon with appropriate type, such as Image or Icon. Just use this property directly in your code, using its fully-qualified name (you can add the using declaration to your file, perhaps alias.

If the type of the file is unknown to Visual Studio, the auto-generated property type will be byte[]; one of the way to use it would be putting the bytes a memory stream an reading the instance of the object from this stream.

I described the general approach you need to use in your cases. As to "My", this is something more specific. If you need to understand what is that, please read:
http://msdn.microsoft.com/en-us/library/bb531245.aspx[^],
http://msdn.microsoft.com/en-us/library/8ffec36z.aspx[^],
http://msdn.microsoft.com/en-us/library/6wkcc526.aspx[^].

—SA


感谢谢谢谢谢你的深入回答......你提出了一些有效的观点,它只是强化了我还有多少要学习:)。



我能够在github的SharpDevelop部分找到一个网页,澄清了这个问题。我想我会在这里为其他有相同问题的人提供一个链接。



LINK https://github.com/icsharpcode/SharpDevelop/wiki/Using-Resources [ ^ ]



特别是这个信息解决了我的问题:



您可以告诉SharpDevelop自动生成一个类,通过在项目浏览器中选择.resx文件,然后打开属性窗口并设置,可以更轻松地访问存储在.resx文件中的资源。自定义工具toResXFileCodeGenerator。然后在项目浏览器中右键单击.resx文件,并选择执行自定义工具。这将为资源文件生成.Designer.cs文件,该文件包含一个用于轻松访问资源的类。每当您更改SharpDevelop中的.resx文件时,将自动重新生成该类。您可以使用ResourceFile.Test_dat/ResourceFile.Image访问资源。
Thanks Sergey for your in-depth answer... you make some valid points and it just reinforces how much I still have to learn :).

I was able to find a webpage at the SharpDevelop section of github which clarified the matter. I thought I'd put a link to it here for others who have the same problem.

LINK https://github.com/icsharpcode/SharpDevelop/wiki/Using-Resources[^]

Specifically this info solved my problem:

You can tell SharpDevelop to automatically generate a class that makes accessing resources stored in .resx files easier by selecting the .resx file in the project browser, then opening the properties window and setting "Custom Tool" to "ResXFileCodeGenerator". Then right click the .resx file in the project browser and choose "Execute custom tool". This will generate a ".Designer.cs" file for the resource file that contains a class for easy access to the resources. The class will automatically be regenerated whenever you change the .resx file inside SharpDevelop. You can access the resources using "ResourceFile.Test_dat" / "ResourceFile.Image".


这篇关于无法通过资源文件更改notifyIcon上的Icon属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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