简体   繁体   English

从代码中访问资源以设置NotifyIcon.Icon

[英]Accessing resources from code for setting NotifyIcon.Icon

I am trying to get the Icon of a NotifyIcon in WPF. 我试图在WPF中获取NotifyIcon的图标。

So I have added a .ico file to my solution in a Resources folder and set the build action to Resource . 所以我在一个Resources文件夹中为我的解决方案添加了一个.ico文件,并将构建操作设置为Resource

I am trying to grab this resource in code behind like so: 我试图在代码后面抓住这个资源,如下所示:

var icon = (Icon) Application.Current.FindResource("/Resources/icon.ico")

This doesn't work. 这不起作用。

In addition to this: Application.Current.Resources.Count returns 0 . 除此之外: Application.Current.Resources.Count返回0

EDIT 编辑

var i = new Icon(Application.GetResourceStream(new Uri("/systemtrayicon.ico", UriKind.Relative)).Stream);

With the icon in the root and the build action set to Resource . 将根中的图标和构建操作设置为Resource

Still not working. 还是行不通。

EDIT AGAIN: 再次编辑:

I needed to Clean the solution and rebuild as per: WPF throws "Cannot locate resource" exception when loading the image 我需要清理解决方案并按照以下方式重建: WPF在加载图像时抛出“无法找到资源”异常

This will works 100% 这将100%有效

ni.Icon = new Icon(Application.GetResourceStream(new Uri("pack://application:,,,<Image Location From root>")).Stream);

Example: 例:

notify.Icon = new Icon(Application.GetResourceStream(new Uri("pack://application:,,,/images/favicon.ico")).Stream);

You have to pass resourceName as a parameter to the FindResource method, not the path for the Resource. 您必须将resourceName作为参数传递给FindResource方法,而不是Resource的路径。 Sample code would look like: 示例代码如下所示:

var icon = (Icon) Application.Current.FindResource("myImage")

Please note in the above sample code "myImage" is the resource name. 请注意,在上面的示例代码中,“myImage”是资源名称。

Refer to Application.FindResource Method on MSDN. 请参阅MSDN上的Application.FindResource方法。

You say, Application.Current.Resources.Count is Zero, that means you do not have any Resource defined in your App.xaml file. 你说,Application.Current.Resources.Count为零,这意味着你没有在App.xaml文件中定义任何资源。

You can add resources to App.xaml like this: 您可以像这样向App.xaml添加资源:

<Application.Resources>
     <Image x:Key="myImage" Source="img.png" />
</Application.Resources>

It appears that your icon is an embedded resource. 您的图标似乎是嵌入式资源。 FindResource cannot work with embedded resources. FindResource无法使用嵌入式资源。 Set BuildAction of your icon to Resource . 将您的图标的BuildAction设置为Resource

Refer to this MSDN page for more reading on WPF Resources. 有关WPF资源的更多信息,请参阅 MSDN页面。

UPDATE UPDATE

Code for accessing Embedded Resources 访问嵌入式资源的代码

Assembly.GetExecutingAssembly().GetManifestResourceStream("myImg.png");

However, if you had added this image to the Resources.Resx and you should simply be able to use Resources.ResourceName . 但是,如果您已将此映像添加到Resources.Resx,您应该只能使用Resources.ResourceName

UPDATE 2 更新2

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或任何ResourceDictionary添加资源更好,因此您可以通过StaticResourceDynamicResource标记扩展将它们用作静态/动态资源。

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 如果您不想将其添加到App.xaml资源并仍想访问它,我上面提到的一个选项是将它添加到Resources.Resx并使用Resources.ResourceName来引用图标/图像

Another way is to create System.Drawing.Icon by yourself, sample code: 另一种方法是自己创建System.Drawing.Icon ,示例代码:

new System.Drawing.Icon(Application.GetResourceStream(new Uri("/Resources/icon.ico")));

Personally, I would go with XAML resources and add them to App.xaml or a ResourceDictionary. 就个人而言,我会使用XAML资源并将它们添加到App.xaml或ResourceDictionary。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM