0
假设您有List<Image>
,则使用类似的方法添加解决方案资源中找到的所有图像;按名称对从资源加载的图像列表进行排序
using (ResourceSet resourceSet = SomeProject.Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true))
{
foreach (DictionaryEntry entry in resourceSet)
{
SomeList.Add((Image)entry.Value);
}
}
在这种情况下,我们的资源中会有三张图片。 Apple.png Banana.png, Cactus.png
如何按字母顺序排列此列表,以便我可以按正确的顺序循环访问,得到Apple -> Banana -> Cactus
?
我读过How to sort ResourceSet in C#,我试图应用于我的情况,但无济于事。
通过我自己,我已经试过分配entry.Key
到图像的标签,然后做someList.OrderBy(f => f.Tag).ToList();
下面是我用它来测试这个
List<Image> someList = new List<Image>();
private void button1_Click(object sender, EventArgs e)
{
using (ResourceSet resourceSet = WindowsFormsApplication52.Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true))
{
foreach (DictionaryEntry entry in resourceSet)
{
Image i = (Image)entry.Value;
i.Tag = entry.Key;
someList.Add((Image)i);
}
someList.OrderBy(f => f.Tag).ToList();
}
foreach(var x in someList)
{
PictureBox pb = new PictureBox();
pb.Image = x;
pb.Size = new System.Drawing.Size(200, 200);
pb.SizeMode = PictureBoxSizeMode.Zoom;
flowLayoutPanel1.Controls.Add(pb);
}
}
这是结果的完整代码,我似乎得到每时间,
我不知道它是否考虑到文件大小,但在这种情况下,图像的文件大小也是按顺序。 (Apple.png是最大的,仙人掌是最小的)。
帮助是极大的赞赏。
您可以使用排序字典为您排序。只需将名称作为密钥添加到它的所有文件。然后做一个foreach循环来按字母顺序检索所有内容。 – deathismyfriend 2014-10-09 16:08:06
您是否看过每个项目上的Tag属性的值,并确认它们正确地命名为Apple,Banana和Cactus? – 2014-10-09 16:11:06
@deathismyfriend我甚至不知道它的存在,我改变了我的列表到一个有序的字典,如你所说,并且..它已经排序!非常感谢你。如果可能,我会将您的评论标记为答案。 Dave Zych是的,我做了,标签设置正确。 – Stella 2014-10-09 16:17:24