从代码中访问资源设置NotifyIcon.Icon [英] Accessing resources from code for setting NotifyIcon.Icon
问题描述
我想获得的的NotifyIcon
WPF中的图标。
I am trying to get the Icon of a NotifyIcon
in WPF.
所以我加了 .ICO
文件到我在资源
文件夹解决方案,并设置构建动作资源。code>
So I have added a .ico
file to my solution in a Resources
folder and set the build action to Resource
.
我想后面抢在这个代码资源,像这样:
I am trying to grab this resource in code behind like so:
VAR =图标(图标)Application.Current.FindResource(/资源/ icon.ico)
这没有按。'T工作
在除了这个: Application.Current.Resources.Count
收益 0
。
修改
VAR I =新图标(Application.GetResourceStream(新的URI(/ systemtrayicon.ico,UriKind.Relative))流。);
通过在根图标设置为资源
生成操作。
With the icon in the root and the build action set to Resource
.
仍然没有工作
再次编辑:
我需要清洁的解决方案,并重建为每: WPF抛出"无法找到资源"例外装载
I needed to Clean the solution and rebuild as per: WPF throws "Cannot locate resource" exception when loading the image
推荐答案
图片时,您必须通过资源名称作为参数传递给FindResource的方法,而不是路径为资源。示例代码如下所示:
You have to pass resourceName as a parameter to the FindResource method, not the path for the Resource. Sample code would look like:
var icon = (Icon) Application.Current.FindResource("myImage")
请注意在上面的示例代码MYIMAGE是资源的名称。
Please note in the above sample code "myImage" is the resource name.
参照的 Application.FindResource 方法MSDN上。
Refer to Application.FindResource Method on MSDN.
您说的,Application.Current.Resources.Count是零,这意味着你没有定义的任何资源。在你App.xaml文件
You say, Application.Current.Resources.Count is Zero, that means you do not have any Resource defined in your App.xaml file.
您可以添加资源的App.xaml像这样的:
You can add resources to App.xaml like this:
<Application.Resources>
<Image x:Key="myImage" Source="img.png" />
</Application.Resources>
看来,你的图标是一个嵌入式的资源。 FindResource
不能与嵌入的资源合作。设置你的图标 BuildAction的
到资源
。
It appears that your icon is an embedded resource. FindResource
cannot work with embedded resources. Set BuildAction
of your icon to Resource
.
请参阅来这个的MSDN页面了解更多阅读WPF的资源。
Refer to this MSDN page for more reading on WPF Resources.
更新
代码访问嵌入式资源
Assembly.GetExecutingAssembly().GetManifestResourceStream("myImg.png");
然而,如果你已经添加了这个形象的Resources.Resx,你应该简单地能够使用 Resources.ResourceName
。
更新2
添加资源的App.xaml或任何ResourceDictionary的比较好,这样就可以通过静态资源
或 DynamicResource
标记扩展。
Adding resources to App.xaml or any ResourceDictionary is better, so that you can use them as Static/Dynamic resources via StaticResource
or DynamicResource
markup extensions.
如果你不想把它加入的App.xaml资源,还是要访问它,一个选项作为我上面提到的是将它添加到 Resources.Resx
,并使用 Resources.ResourceName
引用的图标/图像
If you do not want to add it to App.xaml resources and still want to access it, one option as I mentioned above is to add it to the Resources.Resx
and use Resources.ResourceName
to refer the icon/image
另一种方法是创建 System.Drawing.Icon
,示例代码:
Another way is to create System.Drawing.Icon
by yourself, sample code:
new System.Drawing.Icon(Application.GetResourceStream(new Uri("/Resources/icon.ico")));
就个人而言,我会去与XAML资源,并将其添加到App.xaml中或ResourceDictionary中。
Personally, I would go with XAML resources and add them to App.xaml or a ResourceDictionary.
这篇关于从代码中访问资源设置NotifyIcon.Icon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!