1
我想顯示一定數量的colorpicker控件,具體取決於xaml resourcedictionary文件中的顏色數量。迭代resourcedictionary xaml文件
由於某種原因,我找不出正確的方法來做到這一點。當通過XAMLReader將其加載到ResourcesDictionary對象時,我不確定什麼是迭代它的最佳方式。
我第一次嘗試將它作爲xml處理,使用XDocument.Elements()在嘗試獲取元素時給出了一個空的IEnumerable。
這樣做的最好方法是什麼? XAML中的
例如:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Edit the FontFamily value to change the application font-->
<FontFamily x:Key="NormalFontFamily">Arial</FontFamily>
<!-- Edit ARGB values (hex) to change the colours used in the application -->
<Color x:Key="NormalForegroundColor" A="0xFF" R="0xFF" G="0xFF" B="0xFF" />
<Color x:Key="NormalForegroundColor80" A="0xFF" R="0xB6" G="0xB5" B="0xB5" />
<Color x:Key="DarkerForegroundColor" A="0xFF" R="0x97" G="0x97" B="0x97" />
<Color x:Key="DarkestForegroundColor" A="0xFF" R="0x76" G="0x76" B="0x76" />
<Color x:Key="NormalBackgroundColor" A="0xFF" R="0x22" G="0x22" B="0x22" />
<Color x:Key="DarkerBackgroundColor" A="0xFF" R="0x19" G="0x19" B="0x19" />
<Color x:Key="LighterBackgroundColor" A="0xFF" R="0x33" G="0x33" B="0x33" />
....
謝謝。 我希望能夠在運行時導入新主題(= resourcedictionary),並能夠編輯它們aswel。所以我不想在我的資源文件中編譯。 – 2010-04-28 09:10:29
如果你想在運行時枚舉一個'ResourceDictionary',你的'XamlReader.Load'方法可以很好地工作。試試這個: 使用(var stream = new FileStream(@「path to your file」,FileMode.Open)) {0} {0} var resourceDictionary =(ResourceDictionary)XamlReader.Load(stream); var colors =(IEnumerable)resourceDictionary [「colors」]; 的foreach(Colour彩色的顏色) { // ... }} 嗯 – wpfwannabe 2010-04-28 09:43:24
,你怎麼會得到單獨的項目,而不是在XAML集合的一部分?在我的問題中編輯了一個xaml示例 它只是我仍然想知道,就我的項目而言,我可以實現它,你可以建議。 – 2010-04-29 08:51:33