wpf 从用于设置 NotifyIcon.Icon 的代码访问资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20374060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing resources from code for setting NotifyIcon.Icon
提问by Cheetah
I am trying to get the Icon of a NotifyIconin WPF.
我试图NotifyIcon在 WPF 中获取 a 的图标。
So I have added a .icofile to my solution in a Resourcesfolder and set the build action to Resource.
因此.ico,我在Resources文件夹中的解决方案中添加了一个文件,并将构建操作设置为Resource.
I am trying to grab this resource in code behind like so:
我试图在后面的代码中获取这个资源,如下所示:
var icon = (Icon) Application.Current.FindResource("/Resources/icon.ico")
var icon = (Icon) Application.Current.FindResource("/Resources/icon.ico")
This doesn't work.
这不起作用。
In addition to this: Application.Current.Resources.Countreturns 0.
除此之外:Application.Current.Resources.Count返回0。
EDIT
编辑
var i = new Icon(Application.GetResourceStream(new Uri("/systemtrayicon.ico", UriKind.Relative)).Stream);
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 在加载图像时抛出“无法定位资源”异常
回答by Shivam Srivastava
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);
回答by sthotakura
You have to pass resourceName as a parameter to the FindResource method, not the path for the Resource. Sample code would look like:
您必须将 resourceName 作为参数传递给 FindResource 方法,而不是资源的路径。示例代码如下所示:
var icon = (Icon) Application.Current.FindResource("myImage")
Please note in the above sample code "myImage" is the resource name.
请注意上面示例代码中的“myImage”是资源名称。
Refer to Application.FindResourceMethod 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. FindResourcecannot work with embedded resources. Set BuildActionof your icon to Resource.
看来您的图标是嵌入式资源。FindResource无法使用嵌入式资源。将BuildAction您的图标设置为Resource。
Refer to thisMSDN page for more reading on WPF Resources.
有关 WPF 资源的更多信息,请参阅此MSDN 页面。
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 StaticResourceor DynamicResourcemarkup extensions.
将资源添加到 App.xaml 或任何 ResourceDictionary 会更好,以便您可以通过StaticResource或DynamicResource标记扩展将它们用作静态/动态资源。
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.Resxand use Resources.ResourceNameto refer the icon/image
如果您不想将其添加到 App.xaml 资源但仍想访问它,我上面提到的一种选择是将其添加到Resources.Resx并用于Resources.ResourceName引用图标/图像
Another way is to create System.Drawing.Iconby 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。

