我想要在我的应用程序(工具栏图标)中的所有表单上共享图像列表的一个实例。我见过以前问过的问题,人们想出了一个用户控件(这是不好的,因为它会创建图像列表的多个实例,从而创建不必要的对象和开销)。在.Net Winforms应用程序中共享图像列表
设计时间支持会很好,但不是非常重要。
在Delphi中这很简单:创建一个DataForm,共享图像,然后关闭。
是否存在C#/ .Net/Winforms variantion?
我想要在我的应用程序(工具栏图标)中的所有表单上共享图像列表的一个实例。我见过以前问过的问题,人们想出了一个用户控件(这是不好的,因为它会创建图像列表的多个实例,从而创建不必要的对象和开销)。在.Net Winforms应用程序中共享图像列表
设计时间支持会很好,但不是非常重要。
在Delphi中这很简单:创建一个DataForm,共享图像,然后关闭。
是否存在C#/ .Net/Winforms variantion?
你可以简单地做一个静态类持有一个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"];
尽管如此,该解决方案没有设计时支持。
你可以像这样使用单例类(见下文)。您可以使用设计器来填充图像列表,然后绑定到任何图像手动列出您的使用。
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;
}
}