2014-01-23 72 views
0

我能夠在一個.resx文件以使用this link代碼來查看的項目列表創建項目的列表或數組WPF:在.resx文件

using System.Collections; 
using System.Globalization; 
using System.Resources; 

... 
string resKey; 
ResourceSet resourceSet = MyResourceClass.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true); 
foreach (DictionaryEntry entry in resourceSet) 
{ 
    resKey = entry.Key.ToString(); 
    ListBox.Items.Add(resKey); 
} 

我現在想做什麼,是創建一個可訪問的列表或數組。我如何去做這件事? 爲了澄清,我不想創建一個Image容器數組,並使用一個循環來加載.resx文件中的圖像。 感謝

+0

arrayS的列表您的意思是?你究竟是什麼意思?你想獲得什麼類型的陣列?從什麼? – EngineerSpock

+0

我不確定爲什麼要在WPF項目中使用.resx文件,除非您正在遷移舊項目。爲什麼不使用WPF中的'ResourceDictionary'? – Praggie

+0

@Praggie我剛剛在這個論壇和MSDN上使用了一個例子。兩者都使用ResourceDictionary。它的工作,我不知道任何其他方法。 – Laserbeak43

回答

1

我真的不知道,我給你的權利,但可能這是你想要的東西:

var resources = new List<string>(); 
foreach (DictionaryEntry entry in resourceSet) 
{ 
    resources.Add(entry.Key.ToString()); 
} 

UPDATE

好吧,那麼這裏是另一種解決方案。您可以遍歷您的resourceSet的值,如果有任何值是Bitmap - 將其轉換爲BitmapImage並添加到您的列表中。像這樣:

var images = resourceSet.Cast<DictionaryEntry>() 
      .Where(x => x.Value is Bitmap) 
      .Select(x => Convert(x.Value as Bitmap)) 
      .ToList(); 

public BitmapImage Convert(Bitmap value) 
{ 
    var ms = new MemoryStream(); 
    value.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); 
    var image = new BitmapImage(); 
    image.BeginInit(); 
    ms.Seek(0, SeekOrigin.Begin); 
    image.StreamSource = ms; 
    image.EndInit(); 

    return image; 
} 
+0

我在_System.Resources.ResourceSet_ – Laserbeak43

+0

中看不到'Select()'方法對不起,已更新的答案。 – JleruOHeP

+0

謝謝,這似乎甚至不是我想要做的。這實際上只是從我的資源獲取信息的一種更簡潔的方式。我想嘗試使某種'System.Drawing.Bitmap'類型的優雅加載到另一個System.Windows.Media.ImageSource類型的列表中。 – Laserbeak43