TLDR:
在启动时创建
ImageList
并通过其索引访问每个图像是否仍然很常见,或者资源是可行的方法吗?
详细问题:
我目前正在开发已经开发了一段时间的软件。它部分是用 Visual Basic 编写的,并且严重依赖
ImageList
类来组织软件每个部分中的所有图像。在应用程序开始时,通过一个巨大的数组创建一个ImageList
,其中包含所有可访问的图像文件的所有名称。每当您需要图像时,您都需要访问该对象并通过列表中图像的索引获取图像。
我知道这在过去可能很有用,因为 Visual Studio 中的资源不方便使用,并且某些控件只能使用这些列表,例如
ToolBar
(已替换为 ToolStrip
)具有 Poperty ImageList
的类,每个按钮仅获取其图像在 ImageList
中的索引。
但是我想知道是否应该放弃这个巨大的
ImageList
,转而在资源中组织这些图像,但我主要不是 .net 开发人员,并且对现代 winform 不太了解。
在启动时创建
ImageList
并通过其索引访问每个图像是否仍然很常见?或者这是过时的/仅用于向后兼容,而今天的资源是可行的方法吗?无论如何,请解释原因。
如果是wpf项目
我心里有两个想法
1) 在窗口的 xaml 和 xaml 代码中添加堆栈面板
例如
StackPanel ImagesStackPanel = new StackPanel();
foreach(Image img in Images) //your images enumerable
{
//here you can add also other elements
Image _img = (Image)XamlReader.Parse(@"<Image Name="{{here goes the image id}}" Height="400" Width="400" /> ");
ImagesStackPanel.Children.Add(_img );
}
_Window.Content = ImagesStackPanel;
2) 如果您不担心 cpu/ram 使用情况 如果您非常关心造型等,您可以执行以下操作
另一个想法是使用网络浏览器或cefsharp浏览器。 使用媒体库的 html 代码创建一个 html 文件,将文件设置为资源,然后从 html 代码中调用它们的资源 url。
您可以在 stringbuilder 中加载 html 代码,并通过替换可以设置为 {{my value}} 的特定值,在 C# 中进行任何您想要的更改
//the html code --> id='{{my id}}' src='{{img url }}'
//the c# code --> _yourStringBuilder.ToString().Replace("{{my id}}",id of the image).Replace("{{img url }}",url of the image)
//** You can add any animation and css style you want**