Multiple Merged Dictionaries

AdamPAdamP AUUniversity ✭✭✭✭✭
edited January 11 in Xamarin.Forms Evolution

Summary

To allow multiple dictionaries to be merged in a Resource Dictionary.

API Changes

Currently the MergedWith property only accepts one type. I don't think this can be changed to provide one and multiple at the same time. Hence for backward compatibility, I would look at obsoleting the MergedWith property and change it to a MergedDictionaries property.

I would implement it as follows, since this is very similar to WPF.

e.g.

<ResourceDictionary.MergedDictionaries>
  <ResourceDictionary Source="theme:Resources1"/>
  <ResourceDictionary Source="theme:Resources2"/>
</ResourceDictionary.MergedDictionaries>

In the ResourceDictionary

This would be obsoleted
public Type MergedWith {

This would be its replacement
public ICollection<Type> MergedDictionaries

Intended Use Case

This would be used when you want to separate out your styles into multiple files for easier grouping.

0
0 votes

In Progress · Last Updated

Assigned to @AdamP to implement.

Posts

  • StephaneDelcroixStephaneDelcroix USInsider, Beta ✭✭✭✭

    when a key is present in both the current RD and the parent RD, we have a rule to decide which one to pick (the current one). What would be the rule when a key is present in both parents ?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Yes Yes Yes... Please implement merge dictionaries in a similar way to WPF. Having a single App.XAML 2,000 lines long is horrible. Anyone that uses Control Templates throughout their app has a very hard time with the current system.

  • AdamPAdamP AUUniversity ✭✭✭✭✭
    edited January 12

    I would go with last one defined wins, same as CSS and WPF.

    MergedDictionary1
    MergedDictionary2
    OnPageDictionary

    Hence if it was defined in all 3 above, the OnPageDictionary would win.

    The collection would load the key and value. When it loads another dictionary, if the key already exists, overwrite it with the new value.

  • NMackayNMackay GBInsider, University ✭✭✭✭✭

    I'd be another in favour of this change :smile:

  • JimBennettJimBennett GBInsider, University, Developer Group Leader ✭✭✭✭

    One addition would be able to control dictionaries based off idiom - so different dictionaries for phone or tablet

  • MichaelRumplerMichaelRumpler ATMember ✭✭✭✭

    I also think that Xamarin.Forms should be as near to WPF as possible. Not only with the MergedDictionaries, but for everything. It would make the transition so much easier.

    And I'm also for idiom/platform dependant dictionaries.

    How about this:

    <ResourceDictionary.MergedDictionaries>
      <On Idiom="Phone">
        <ResourceDictionary Source="Resource1"/>
      </On>
      <On Platform="iOS">
        <ResourceDictionary Source="Resource2"/>
      </On>
      <On Platform="WinPhone,UWP,WinRT">
        <ResourceDictionary Source="Resource3"/>
      </On>
    </ResourceDictionary.MergedDictionaries>
    

    Although that On could be moved to a separate thread and used everywhere.

  • MarkFeldmanMarkFeldman USMember ✭✭

    Yes please! This really is a surprising omission from the current code base, I've never worked on either a WPF or Xamarin project that didn't require this in some form or another.

  • PhilippSumiPhilippSumi USMember ✭✭✭

    +1 on this. The current merging is pretty useless, while multiple dictionaries allow us to organize our assets rather than squashing everything into a big app.xaml file.

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    Raising my hand to implement this, if the API change is approved by the XF team.

  • DavidOrtinauDavidOrtinau USForum Administrator, Xamarin Team, Insider, University Xamurai

    Thanks @AdamP! We're discussing and will let you know shortly.

  • BurtBurt USMember

    I was just trying this sort of things when I found this. Apparently this is even an actual issue.

    I wanted to have more specific ResourceDictionaries on top of a base ResourceDictionary

    • At first I hoped to nest the base in the specific by chaining from the specific to the base by MergedWith.
    • I also tried to nest a dictionary merged to base within the specific.
    • Finally I tried to nest both base and specific within an other to combine them.

    As you guessed, that all failed before I found this topic.

    One way or another, me too would appreciate some form or nested, chained or multiple merging.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    The 'spec' for merge dictionary already exists. There is no need to guess about what it should be.
    https://msdn.microsoft.com/en-us/library/aa350178(v=vs.110).aspx

    As many have said in this any other threads: Priority 1 should be to bring Xamarin up to par with WPF. That gives millions of existing WPF developers a smooth migration and a really solid user base of EXPERIENCED XAML/MVVM developers.

    Microsoft now owns Xamarin. They are one big happy family, just ask 'em. So sharing code and making this happen shouldn't be a huge, long, lengthy, drawn-out process. I bet there might be one or two sharp cookies over there that could make a tool/converter that could bring a lot of that 10+ year proven WPF code into the Xamarin eco-system.

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    It will have to be a little different for Xamarin Forms due to two factors.

    1. WPF is using a file name, where as XF uses namespace/assembly, hence the xmlns prefix
    2. MergedWith will need to be obsoleted but consideration on how they interact while both are enabled is needed.

    Other than that, I agree the API should be the same in declaring and in which key takes priority. Its actually not that big of a change code behind the scenes, its just loading the all the resource key's and values into the already existing resource list.

  • TorstenSeemannTorstenSeemann DKUniversity ✭✭

    +1 more. Merging multiple RDs would make a lot more sense than the current approach. Especially in larger solutions where organizing resources into separate RD files for different purposes is often necessary.

  • predalphapredalpha FRMember ✭✭
    edited March 9

    +1
    It will be more UWP geared. More its close to UWP development better it is !

  • LewisKLewisK USUniversity ✭✭

    @DavidOrtinau Has there been any updates regarding the discussion?

  • DavidOrtinauDavidOrtinau USForum Administrator, Xamarin Team, Insider, University Xamurai

    @AdamP thanks for stepping up! Please move forward with implementing and feel free to reach out to discuss as needed.

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    thanks @DavidOrtinau - I will get to it :)

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    Update: Finished coding, all works well :) Just adding more Unit Tests and checking perf, no memory leaks etc. Will aim for a PR sometime this week.

  • DavidDancyDavidDancy AUMember ✭✭✭✭

    @AdamP I came across a scenario recently when using a ControlTemplate where the ResourceDictionary from the app wasn't merged in (even though I said it should be). Does your PR work in this situation too? I can share more details if needed.

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    @DavidDancy - I haven't looked at anything with ControlTemplates and won't be for this PR. However I could look at it, and submit another PR, if its not assigned to any Xamarin Team Member. Provided it doesn't take me too much time to track.

  • DavidDancyDavidDancy AUMember ✭✭✭✭

    @AdamP Great, thanks!

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    @DavidDancy - do you have a link to a bug report or any more information on what is happening?

  • DavidDancyDavidDancy AUMember ✭✭✭✭

    @AdamP I have a (private) repo of a project that suffers from the issue. I use a ControlTemplate for all pages, and wanted to have the app's styles and other resource items available to it. However when I added the merge details to the ControlTemplate's ResourceDictionary it didn't work. I forget exactly, but it may even have crashed the app. Shall I see if I can make a small repro for you?

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    @DavidDancy - if you can make a small repro and submit it to bugzilla, that would be even better. Xamarin are unlikely to accept any PR for a bug fix, unless its been recorded and confirmed on bugzilla.

  • DavidDancyDavidDancy AUMember ✭✭✭✭

    @AdamP So I think I must have mis-remembered - I just tried to re-create the problem and failed. But in my defence the repro attempt was with Forms 2.3.4.

    However I do have a case where if I put my ControlTemplate declaration in a ResourceDictionary, and merge that with the app's ResourceDictionary, the app crashes. Putting the ControlTemplate declaration directly into the app's ResourceDictionary is the only way I can find to make it work.

    I have a repro project here: https://bugzilla.xamarin.com/show_bug.cgi?id=54889

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    I always eat my words, whenever I say something is easy to implement :)

    https://github.com/xamarin/Xamarin.Forms/pull/869

  • KosKos UAMember

    In Xamarin.Forms 2.5.0.19271-pre2 it doesn't work.
    I have empty dictionaries. no styles, no templates.

Sign In or Register to comment.