2016-04-15 46 views
3

是否可以枚舉組件中定義的所有XAML資源?我知道如何獲取一個資源,如果你有它的密鑰可用,但在我的情況並非如此。編輯: 似乎我不夠清楚。我想列出在我知道完整路徑的大會外部大會中定義的XAML資源。如何獲取組件中定義的XAML資源列表?

+0

你想要所有的資源,無論它們是在app/window/usercontrol/style中定義的嗎? – Nitin

+0

我只需要外部可用的資源。我不認爲你可以使用另一個程序集的UserControl/Window中定義的資源,對吧? – ionoy

回答

5

是的,你可以通過循環迭代資源。例如,使用foreach循環:

foreach (var res in Application.Current.Resources) 
{ 
    Console.WriteLine(res); 
} 

更新:

從外部庫得到所有ResourceDictionary'ies,你應該在第一,加載庫,然後得到ManifestResourceInfo。讓我給個例子:

string address = @"WpfCustomControlLibrary.dll"; 
List<Stream> bamlStreams = new List<Stream>(); 
Assembly skinAssembly = Assembly.LoadFrom(address);    
string[] resourceDictionaries = skinAssembly.GetManifestResourceNames(); 
foreach (string resourceName in resourceDictionaries) 
{ 
    ManifestResourceInfo info = skinAssembly.GetManifestResourceInfo(resourceName); 
    if (info.ResourceLocation != ResourceLocation.ContainedInAnotherAssembly) 
    { 
     Stream resourceStream = skinAssembly.GetManifestResourceStream(resourceName); 
     using (ResourceReader reader = new ResourceReader(resourceStream)) 
     { 
     foreach (DictionaryEntry entry in reader) 
     { 
      //Here you can see all your ResourceDictionaries 
      //entry is your ResourceDictionary from assembly 
      } 
     } 
    } 
} 

你可以看到所有你ResourceDictionary的在reader。請參閱上面的代碼。

我測試過這段代碼,它工作。

+0

對不起,好像我不清楚我的問題。我想列出當前未加載到AppDomain中的外部程序集中的資源。 – ionoy

+0

不幸的是,我不認爲它會列出在XAML中定義的資源。 – ionoy

+0

@ionoy請看我更新的答案。我已經測試了這個代碼,它的工作原理。 – StepUp

4

試試下面的代碼:

 ResourceDictionary dictionary = new ResourceDictionary(); 
     dictionary.Source = new Uri("pack://application:,,,/WpfControlAssembly;Component/RD1.xaml", UriKind.Absolute); 
     foreach (var item in dictionary.Values) 
     { 
      //Operations 
     } 

這裏WpfControlAssembly是您的程序集的名稱。 Component是固定值,然後RD1.xamlResource Dictionary

下面是輸出:

資源字典

Resource Dictionary

代碼輸出:

Output

PS:全部ResourceDictionary文件應該有Build Action作爲'Resource''Page'

更新:

最後我能做到這一點。請使用以下方法:

public ResourceDictionary GetResourceDictionary(string assemblyName) 
    { 
     Assembly asm = Assembly.LoadFrom(assemblyName); 
     Stream stream = asm.GetManifestResourceStream(asm.GetName().Name + ".g.resources");    
     using (ResourceReader reader = new ResourceReader(stream)) 
     { 
      foreach (DictionaryEntry entry in reader) 
      { 
       var readStream = entry.Value as Stream; 
       Baml2006Reader bamlReader = new Baml2006Reader(readStream); 
       var loadedObject = System.Windows.Markup.XamlReader.Load(bamlReader); 
       if (loadedObject is ResourceDictionary) 
       { 
        return loadedObject as ResourceDictionary; 
       } 
      } 
     } 
     return null; 
    } 

OUTPUT:

RD

沒有預期Exceptions任何的try-catch &,我認爲更多的WPF(而不是將一切到ResourceDictionary)的方式。

+0

但是如果我不知道ResourceDictionary(RD1.xaml)的名稱會怎麼樣?是否有可能在程序集中找到所有ResourceDictionaries的名稱? – ionoy

+1

如果您不知道ResourceDictionaries的名稱,可以從AssamblyInfo獲得該名稱,但是如果您不知道名稱,您將如何找到它們中的資源。我的意思是如果你不知道。資源詞典的名字,但知道它們的資源名稱似乎對我來說很諷刺。我不確定你想在這裏實現什麼。 –

+0

@ionoy你對這個大會有什麼細節? –

相關問題