对从 resx 文件 C# 中提取的所有图像进行字母处理

本文关键字:图像 处理 文件 resx 提取 对从 | 更新日期: 2023-09-27 18:30:58

我有一个下拉组合框,可以更改图片框的图像。图像存储在一个resx文件中,其中包含一个对它们进行计数的数组,因此,如果我决定添加更多图像,我只需要更新组合框,并将图像添加到resx文件中。我遇到的问题是,当我使用组合框更新图像时,resx 中的图像不是按字母顺序排列的,但它们确实会更改图片框上的图像。

这是我的代码

        ResourceSet cardResourceSet = Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
        attributes = new Image[cardCount];
        cardCount = 0;
        foreach (DictionaryEntry entry in cardResourceSet)
        {
            string resourceKey = (string)entry.Key;
            object resource = entry.Value;
            cardCount++;
        }
        attributes = new Image[cardCount];
        cardCount = 0;
        foreach (DictionaryEntry entry in cardResourceSet)
        {
            attributes[cardCount] = (Image)entry.Value;
            cardCount++;
        }
        if (attributeBox.SelectedIndex != -1)
        {
            this.cardImage.Image = attributes[attributeBox.SelectedIndex];
        }

如何让它按字母顺序对 resx 中的资源进行排序?

对从 resx 文件 C# 中提取的所有图像进行字母处理

GetResourceSet

返回类型,ResourceSet ,实现IEnumerable,所以你应该能够对它运行一些 LINQ 排序:

foreach (var entry in cardResourceSet.Cast<DictionaryEntry>().OrderBy(de => de.Key))
{
}

由于您要迭代它两次(尽管我不太确定第一个 for 循环的重点,除非有其他代码),因此您可能希望将排序结果分配给一个单独的变量:

var sortedCardResourceSet.Cast<DictionaryEntry>().OrderBy(de => de.Key).ToList();
foreach (var entry in sortedCardResourceSet)
{
    ...
}
...