我有一些过去可以工作的代码现在不能工作(不知道是 SL4 -> SL5 还是其他导致此问题的更新)。
我正在为右键单击菜单即时创建 MenuItems,并且我正在分配图标,如下所示:
public static class XamlUtility
{
public static string makeResourcePngUri(string sName)
{
return "/MyApplication;component/Resources/" + sName + ".png";
}
public static BitmapImage getBitmapImageFromResources(string sName)
{
BitmapImage _bmi = null;
_bmi = new BitmapImage(new Uri(makeResourcePngUri(sName), UriKind.Relative));
return _bmi;
}
public static Image getImageFromResources(string sName)
{
Image _im = new Image();
_im.Source = getBitmapImageFromResources(sName);
return _im;
}
}
public static class XamlUtility
{
public static string makeResourcePngUri(string sName)
{
return "/MyApplication;component/Resources/" + sName + ".png";
}
public static BitmapImage getBitmapImageFromResources(string sName)
{
BitmapImage _bmi = null;
_bmi = new BitmapImage(new Uri(makeResourcePngUri(sName), UriKind.Relative));
return _bmi;
}
public static Image getImageFromResources(string sName)
{
Image _im = new Image();
_im.Source = getBitmapImageFromResources(sName);
return _im;
}
}
...
public static MenuItem addMenuItem(ContextMenu ctxmenu, string name, bool visible = true, MenuClickHandler clickHandler = null, string imageName = null)
{
string _name = name.Replace(' ', '_');
MenuItem menu = new MenuItem()
{
Name = "mnu" + _name,
Header = name,
Icon = XamlUtility.getImageFromResources(imageName ?? _name)
};
...
该图像仅在我之前加载时才加载(例如,在生成 ContextMenu 之前在某些 xaml 表单上创建图像控件)。
这曾经有效(修复的优先级不是很高,所以我不知道它什么时候停止工作)