Embed Icon in WPF application

試著忘記壹切 提交于 2019-12-11 07:23:33

问题


I am using an icon in my application in two situations.

  1. In XAML as an Image for the Button using DynamicResource.
  2. In C# as NotifyIcon

    this.notifyIcon.Icon = new SystemDrawing.Icon("..//..//Shutdown.ico" );

My problem is if I delete the image the application not working. How can I bind the image with the exe file so that the application can run only with the exe file?


回答1:


Add the icon to the project as a Resource (not Embedded Resource, there is a difference), then access it with

using(Stream stream = Application.GetResourceStream(new Uri("/Shutdown.ico")).Stream)
{
    this.notifyIcon.Icon = new SystemDrawing.Icon(stream);
}

You should also then be able to use it in your XAML too, I just can't remember how yet.

See here for more information: http://msdn.microsoft.com/en-us/library/aa970494.aspx

Note: I have not included any error checking here. You should probably make sure you check the stream to see if it is null before using it.




回答2:


The last time I needed to do this, the image was so simple that it was easiest to simply create a line drawing in XAML (similar to SVG) instead of using a file. I was attempting to build a simple Vista-style wizard without reusing anyone else's code (to see if I could), and I wanted the arrow of a command button as can be seen in the UX guidelines on MSDN.



来源:https://stackoverflow.com/questions/985003/embed-icon-in-wpf-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!