searching and listing WPF resource dictionaries in a folder

前端 未结 3 699
失恋的感觉
失恋的感觉 2021-01-19 07:10

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

3条回答
  •  借酒劲吻你
    2021-01-19 07:35

    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)
       {
        .....
       }
      }
     }
    }
    

提交回复
热议问题