Silverlight MenuItem图标仅在图像已加载时加载图像

时间:2013-01-26 16:05:34

标签: silverlight icons contextmenu menuitem

我有一些以前工作的代码,现在却没有(不知道是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表单上创建图像控件)。

这曾经工作过(修复不是一个高优先级所以我不知道它何时停止工作)

1 个答案:

答案 0 :(得分:2)

在你的getBitmapImageFromResources方法中添加

_bmi.CreateOptions = BitmapCreateOptions.IgnoreImageCache;