Allow Merged Dictionaries hierarchically merging

MakemanMakeman BYMember
edited November 9 in Xamarin.Forms Evolution

[Sorry for links format, I have not permission to post correct links...]
[Moved from "bugzilla.xamarin.com/show_bug.cgi?id=60316" by suggestion of Paul DiPietro]

Summary

WPF and UWP allow to merge dictionaries hierarchically. It is very useful feature which helps to split resources by different logically files.
Current Xamarin.Forms implementation of Merged Dictionaries at master brunch can not merge dictionaries hierarchically.

Just try to run following method on master and WPF or UWP for tests.

    public static void MergedDictionariesDeepSublevels()
    {
        var rd0 = new ResourceDictionary();
        var rd1 = new ResourceDictionary { { "Key1", "Level1" } };
        var rd2 = new ResourceDictionary { { "Key2", "Level2" } };

        rd0.MergedDictionaries.Add(rd1);
        rd1.MergedDictionaries.Add(rd2);

        Assert.That(rd0["Key1"], "Level1");
        Assert.That(rd0["Key2"], "Level2"); // <= fail on master, work on WPF or UWP

        //Assert.That(rd0.ContainsKey("Key1"), Is.EqualTo(true));
        //Assert.That(rd0.ContainsKey("Key2"), Is.EqualTo(true));
    }

Fix is very simple, see commit
github.com/xamarin/Xamarin.Forms/commit/e22367ebe90783579fa4a7b3b66da196d41a1960

Full PR
github.com/xamarin/Xamarin.Forms/pull/1218

API Changes

There is not API changes, only removal of restrictions the current implementation.

Intended Use Case

Usually I group different types of resources by files and merge, for example:
1) Colors.xaml=>Paints.xaml
2) Paints.xaml=>Templates.xaml
3) Converters.xaml=>Templates.xaml
4) Templates.xaml=>Styles.xaml
5) Styles.xaml=>App.xaml

At the current moment I use own Customized Merged Dictionary for Xamarin Forms, but it is implementation is not so beautiful (direct coping merged values into root dictionary). See for details github.com/Makeloft/Ace/blob/master/Ace.Zest/Markup/ResourceDictionary.cs

Posts

  • NMackayNMackay GBInsider, University ✭✭✭✭✭

    This is available from 2.5.0.19271-pre2.

    Adam's PR was merged a while back.

  • MakemanMakeman BYMember
    Please, see details by links. Yes, Merged Dictionaries added by Adam, but there is not support hierarchically merging, only one level (see Adam’s comment on bugzilla).
  • MakemanMakeman BYMember

    Hey, guys! Can anybody review suggested pull request ?
    github.com/xamarin/Xamarin.Forms/pull/1218

  • Want to throw my support behind this. This is great for complex reusable style libraries or sophisticated apps w/ theming behavior.

Sign In or Register to comment.