【问题标题】:Silverlight MenuItem icon only loads image if image already loadedSilverlight MenuItem 图标仅在图像已加载时才加载图像
【发布时间】:2013-01-26 16:05:34
【问题描述】:

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

这曾经可以工作(修复的优先级不高,所以我不知道它什么时候停止工作)

【问题讨论】:

    标签: silverlight icons contextmenu menuitem


    【解决方案1】:

    在你的 getBitmapImageFromResources 方法中添加

    _bmi.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
    

    【讨论】:

    • 谢谢。完美运行(尽管我仍然对为什么我以前从未需要它以及为什么它只在我的上下文菜单中需要它感到困惑)。
    • 我不知道为什么。如果我有一些空闲时间,我会给它更深入的了解。很高兴它对你有效。认为您可以将其标记为已回答?
    • 哎呀...对此感到抱歉(已回答的部分)。只是有点好奇。它正在工作(我只将 ignorecache 用于它不起作用的实例,以防万一有性能优势)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    相关资源
    最近更新 更多