searching and listing WPF resource dictionaries in a folder

主宰稳场 提交于 2019-12-19 20:48:34

问题


I'm making a WPF application that is going to have multiple skins branded in by our build system. Ideally we would like the application to list off the skins available as some builds will have one to many skins.

At runtime is there a way to enumerate all the Resource Dictionaries in a specific folder?

I want to avoid hard coding the XAML filenames in my codebehind as this is a changing situation.


回答1:


Sort of.

You can enumerate all BAML (compiled XAML) files as follows:

  var resourcesName = assembly.GetName().Name + ".g";
  var manager = new System.Resources.ResourceManager(resourcesName, assembly);
  var resourceSet = manager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
  var allXamlFiles =
    from entry in resourceSet.OfType<DictionaryEntry>()
    let fileName = (string)entry.Key
    where fileName.EndsWith(".baml")
    select fileName.Substring(0, fileName.Length-5) + ".xaml";

There is no way to know which ones of these are ResourceDictionaries and which are other XAML such as Windows or UserControls without actually loading them. So the answer to your direct question is "no" unless you load each XAML you find to check if it is a ResourceDictionary. This would be very slow.

On the other hand, if you're willing to use a naming scheme for your ResourceDictionaries, then you can enumerate all BAMLs in your assembly and select whichever match your naming scheme, and trust that they are ResourceDictionaries. Just extend the "where" clause in the above query.

Therefore the answer is "sort of."




回答2:


here is a solution i found @ microsoft

WPF wraps the ResourceDictionary in the "Assembly.g.resources", we can get the resource name "Assembly.g.resources" by GetManifestResourceNames(). After that we can use ResourceReader class to read the Resource from the ResourceStream.

 foreach (string str in Application.ResourceAssembly.GetManifestResourceNames()){
txt.Text += str + "\n";
 {
  Stream st = Application.ResourceAssembly.GetManifestResourceStream(str);
  using (ResourceReader resourceReader = new ResourceReader(st))
  {
   foreach (DictionaryEntry resourceEntry in resourceReader)
   {
    txt.Text +="\t: "+ resourceEntry.Key + "\n";
   }
  }
 }
}

second answer

You can use Assembly.Load() to load the external assembly and read the resource dictionaries from it.

Assembly assembly = Assembly.Load("Assembly Name String"); 
foreach (string str in assembly.GetManifestResourceNames())
{
 {
  Stream st = assembly.GetManifestResourceStream(str);
  using (ResourceReader resourceReader = new ResourceReader(st))
  {
   foreach (DictionaryEntry resourceEntry in resourceReader)
   {
    .....
   }
  }
 }
}



回答3:


You can add ResourceDictionary at runtime.

Resources.MergedDictionaries.Add(...)


来源:https://stackoverflow.com/questions/1765920/searching-and-listing-wpf-resource-dictionaries-in-a-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!