2009-07-17 4 views
3

내 응용 프로그램 (도구 모음 아이콘)의 모든 양식에서 공유 할 이미지 목록 인스턴스를 하나 갖고 싶습니다. 이전에 물어 본 질문을 보았고 사용자가 사용자 컨트롤을 생각해 냈습니다.이 컨트롤은 여러 가지 인스턴스를 생성하여 불필요한 객체와 오버 헤드를 생성하므로 좋지 않습니다..Net Winforms 응용 프로그램 공유

디자인 타임 지원은 좋지만 필수적인 것은 아닙니다.

델파이에서는 데이터 폼을 만들고 이미지를 공유하면 꺼집니다.

거기에 C#/.Net/Winforms 변형이 있습니까?

답변

5

당신은 단순히 정적 클래스가의 ImageList 인스턴스를 유지하게하고, 응용 프로그램에서 그것을 사용할 수있는 것 같아요 :

public static class ImageListWrapper 
{ 
    static ImageListWrapper() 
    { 
     ImageList = new ImageList(); 
     LoadImages(ImageList); 
    } 

    private static void LoadImages(ImageList imageList) 
    { 
     // load images into the list 
    } 

    public static ImageList ImageList { get; private set; } 
} 

그런 다음 당신은 호스팅의 ImageList에서 이미지를로드 할 수

someControl.Image = ImageListWrapper.ImageList.Images["some_image"]; 

하지만 그 솔루션에서는 디자인 타임을 지원하지 않습니다.

3

이렇게 싱글 톤 클래스를 사용할 수 있습니다 (아래 참조). 디자이너를 사용하여 이미지 목록을 채우고 수동으로 사용하는 이미지 목록에 바인딩 할 수 있습니다.


using System.Windows.Forms; 
using System.ComponentModel; 

//use like this.ImageList = StaticImageList.Instance.GlobalImageList 
//can use designer on this class but wouldn't want to drop it onto a design surface 
[ToolboxItem(false)] 
public class StaticImageList : Component 
{ 
    private ImageList globalImageList; 
    public ImageList GlobalImageList 
    { 
     get 
     { 
      return globalImageList; 
     } 
     set 
     { 
      globalImageList = value; 
     } 
    } 

    private IContainer components; 

    private static StaticImageList _instance; 
    public static StaticImageList Instance 
    { 
     get 
     { 
      if (_instance == null) _instance = new StaticImageList(); 
      return _instance; 
     } 
    } 

    private StaticImageList() 
     { 
     InitializeComponent(); 
     } 

    private void InitializeComponent() 
    { 
     this.components = new System.ComponentModel.Container(); 
     this.globalImageList = new System.Windows.Forms.ImageList(this.components); 
     // 
     // GlobalImageList 
     // 
     this.globalImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit; 
     this.globalImageList.ImageSize = new System.Drawing.Size(16, 16); 
     this.globalImageList.TransparentColor = System.Drawing.Color.Transparent; 
    } 
} 
관련 문제