问题
I am using an icon in my application in two situations.
- In XAML as an Image for the Button using DynamicResource.
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