Home > Back-end >  How could one to merge resource dictionaries either dynamically or in compile time?
How could one to merge resource dictionaries either dynamically or in compile time?

Time:11-07

Let's say we have a resource dictionary defined like

<Application.Resources>
   <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="Resources/Styles/Colors.xaml" />
          <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
      </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>

If I would like to switch Android specific libraries, as for the sake of example define this like

<Application.Resources>
   <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
          <!-- There seem to be no way to choose based on platform.
          <ResourceDictionary Source="Resources/Styles/Colors.xaml" />
          <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
          -->
          <ResourceDictionary Source="Resources/Styles/Android/Colors.xaml" />
          <ResourceDictionary Source="Resources/Styles/Android/Styles.xaml" />
      </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>

Is there an official way to do it on compile time based on target (so, multitargeting and compiling to Android)?

If I try enter image description here

Result on Windows:

enter image description here

UPDATE

This approach works, I only noticed that the newly created XAML files might disappear from the Solution Explorer, but they still get processed during the build. Apparently, the <MauiXaml> build action does not get reevaluated in the Solution Explorer according to the selected target framework. I regard this as an inconvenience and I'm happy to update the answer if I find a better solution or if someone has suggestions to improve it.

  • Related