代码之家  ›  专栏  ›  技术社区  ›  Dennis

处理大量重复图标的资源

  •  1
  • Dennis  · 技术社区  · 17 年前

    我正在使用带有图像列表的树状视图。由于树视图中充满了项目,因此每个项目都有一个与其关联的图标。其中许多图标可能是相同的,但目前,我没有添加任何代码来跟踪重复项,我只是将每个图标添加到ImageList中,然后跟踪与每个树节点关联的图标。

    我的问题是关于.Net如何处理这些资源。例如,运行时是否意识到某些图标完全相同,因此对所有重复的图标只加载一次相同的图标?如果不是,如果这样做了数万次,我是否会遇到资源问题(这不是典型情况,但可能会发生)?

    1 回复  |  直到 17 年前
        1
  •  2
  •   Scott Dorman    17 年前

    该框架没有任何机制来处理这样的资源,以确保不会加载重复项。在树视图的情况下尤其如此,因为它使用的ImageList将这些图像作为包含树视图的表单的本地资源进行维护。

    public sealed class ImageListManager
    {
        private static volatile ImageListManager instance;
        private static object syncRoot = new object();
        private ImageList imageList;
    
        private ImageListManager()
        {
            this.imageList = new ImageList();
            this.imageList.ColorDepth = ColorDepth.Depth32Bit;
            this.imageList.TransparentColor = Color.Magenta;
        }
    
        /// <summary>
        /// Gets the <see cref="System.Windows.Forms.ImageList"/>.
        /// </summary>
        public ImageList ImageList
        {
            get
            {
                return this.imageList;
            }
        }
    
         /// <summary>
         /// Adds an image with the specified key to the end of the collection if it 
         /// doesn't already exist.
         /// </summary>
         public void AddImage(string imageKey, Image image)
         {
            if (!this.imageList.ContainsKey(imageKey))
            {
               this.imageList.Add(imageKey, image);
            }
         }
    
        /// <summary>
        /// Gets the current instance of the 
        /// <see cref="ImageListManager"/>.
        /// </summary>
        public static ImageListManager Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new ImageListManager();
                        }
                    }
                }
                return instance;
            }
        }
    }
    

    然后给树视图一个对该ImageList的引用,通常在调用 InitializeComponent() .

    public partial class Form1 : Form
    {
       public Form1()
       {
          InitializeComponent();
          this.treeView1.ImageList = ImageListManager.Instance.ImageList;
       }
    
       // Making some guesses about how you are adding nodes
       // and getting the associated image.
       public void AddNewTreeNode(string text, string imageKey, Image image)
       {
          TreeNode node = new TreeNode("display text");
          node.Name = "uniqueName";
    
          // This tells the new node to use the image in the TreeView.ImageList
          // that has imageKey as its key.
          node.ImageKey = imageKey;
          node.SelectedImageKey = imageKey;
    
          this.treeView1.Nodes.Add(node);
    
          // If the image doesn't already exist, this will add it.
          ImageListManager.Instance.AddImage(imageKey, image);
       }
    
    }
    
    推荐文章