Include an icon for a Prism module
I want my Prism shell to display an image and label for each module that it discovers. How might I go about including an image from a module assembly and accessing it in the shell?
I've already tried creating an interface for providing an icon (as an ImageSource
) and a string label, but I am having trouble creating the ImageSource
from the image in the module's assembly. (The URI used in the constructor for a BitmapImage
always 开发者_开发百科wants to find content in the shell's assembly rather than the module's assembly.)
I was thinking that I could add an image as a resource and just use that, but it is represented in code as a System.Drawing.Bitmap
and I couldn't find an obvious way to convert that to a type usable in the WPF shell. (I did see some code for converting from a Bitmap
, but that feels like the wrong approach. Might have to settle with that, though.) [Edit: That approach does actually work, but I still think it smells.]
Pat, your problem is most likely because you do not use the correct path to the image. You are trying to load that image in the view constructor or something then it does not find the import fails. You wanna double check whether your image is compiled as Resource. Use this (assuming your image is called Icon.png and is locatd in the root of your module project)
public BitmapImage Icon
{
get
{
var assembly = Assembly.GetCallingAssembly();
Uri uri = new Uri("/" + GetType().Assembly.ToString().Split(',')[0] + ";component/Icon.png", UriKind.Relative);
if (_icon == null)
_icon = new BitmapImage(uri);
return _icon;
}
}
When you return the BitmapSource
from the module, make sure to use a Pack Uri. If you are trying to load an image from an assembly ModuleFoo.dll, you would do something like so:
public ImageSource ModuleImage
{
get
{
return BitmapFrame.Create(new Uri("pack://application:,,,/ModuleFoo;component/ModuleIcon.png", UriKind.Absolute);
}
}
Remember that for WPF resources, the images must be compiled as a "Resource", and not "Embedded Resource" that WinForms requires.
精彩评论