searching and listing WPF resource dictionaries in a folder

前端 未结 3 751
失恋的感觉
失恋的感觉 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条回答
  •  Happy的楠姐
    2021-01-19 07:34

    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()
        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."

提交回复
热议问题