Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按名称对从资源中加载的图像列表进行排序_C#_Sorting_Visual Studio 2013 - Fatal编程技术网

C# 按名称对从资源中加载的图像列表进行排序

C# 按名称对从资源中加载的图像列表进行排序,c#,sorting,visual-studio-2013,C#,Sorting,Visual Studio 2013,假设您有一个列表,您可以使用类似的方式添加在解决方案资源中找到的所有图像 using (ResourceSet resourceSet = SomeProject.Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true)) { foreach (DictionaryEntry entry in resourceSet)

假设您有一个
列表
,您可以使用类似的方式添加在解决方案资源中找到的所有图像

using (ResourceSet resourceSet = SomeProject.Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true))
        {
            foreach (DictionaryEntry entry in resourceSet)
            {
                SomeList.Add((Image)entry.Value);
            }
        }
在本例中,我们的资源中将有三个图像
Apple.png香蕉.png,Cactus.png

如何按字母顺序对列表进行排序,以便按正确的顺序循环浏览,获得
Apple->Banana->Cactus

我读过一些书,我试图将其应用于我的情况,但没有用

我自己尝试将
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);
        }
    }
List someList=newlist();
私有无效按钮1\u单击(对象发送者,事件参数e)
{
使用(ResourceSet ResourceSet=WindowsFormsApplication52.Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture,true,true))
{
foreach(资源集中的DictionaryEntry条目)
{
Image i=(Image)entry.Value;
i、 Tag=entry.Key;
添加((图像)i);
}
OrderBy(f=>f.Tag.ToList();
}
foreach(someList中的变量x)
{
PictureBox pb=新PictureBox();
pb.Image=x;
pb.Size=新系统图纸尺寸(200200);
pb.SizeMode=PictureBoxSizeMode.Zoom;
flowLayoutPanel1.Controls.Add(pb);
}
}
这是我每次都能得到的结果,

我不知道它是否考虑了文件大小,但在这种情况下,图像的文件大小也是有序的。(Apple.png最大,仙人掌最小)


非常感谢您的帮助。

您可以将所有图像添加到以图像名称为键的已排序词典中。
然后对字典执行foreach操作,按名称的字母顺序检索图像

您可以使用已排序的词典为您进行排序。只需将所有文件以名称作为键添加到其中。然后进行foreach并按字母顺序循环检索所有内容。您是否查看了每个项目上的
标记
属性值,并确认它们正确命名为
苹果
香蕉
仙人掌
?@deathismyfriend我甚至不知道它的存在,如你所说,我把我的单子改成了一本分类词典。。已经分类了!非常感谢你。如果可以的话,我会把你的评论作为回答。Dave Zych是的,我做了,标签设置正确。@stella。我回答了。如果您愿意接受,我们将不胜感激。很高兴我能帮忙。