2010-08-06 22 views
1

上午鄉親,WPF動態圖像加載到ResourceDictionary的編程

我一直在試圖削減了一些我的應用程序的處理,當我偶然發現在SO建議通過BitmapImages的所有圖像加載到一個資源字典,然後引用它們而不是每次加載圖像。我的問題是,我需要做的這一切編程,所以:

<BitmapImage x:Key="MyImageSource" UriSource="../Media/Image.png" /> 

已成爲:

BitmapImage bI = new BitmapImage(); 
Uri imgUri = new Uri(fI.FullName, UriKind.RelativeOrAbsolute); 
bI.UriSource = imgUri; 
DataTemplateKey dTK = new DataTemplateKey(typeof(BitmapImage)); 
imageDictionary.Add(dTK, bI); 

我想這應該工作,但是因爲它可以將由於圖像裝車基於數據庫內容,我立即得到一個關鍵字,在第二個循環中已經添加了錯誤。有沒有什麼辦法可以創建一個我可以自己命名的數據模板鍵,而不是基於類型?

感謝, 貝基

回答