Fix dynamic resources in merged dictionaries #3661 (#4993) * fixes #3661
authorPatrick Allwood <patchandthat@gmail.com>
Mon, 18 Feb 2019 14:57:23 +0000 (14:57 +0000)
committerRui Marinho <me@ruimarinho.net>
Mon, 18 Feb 2019 14:57:23 +0000 (14:57 +0000)
Xamarin.Forms.Core/ResourcesExtensions.cs

index 1962c3a..9c567ca 100644 (file)
@@ -15,14 +15,19 @@ namespace Xamarin.Forms
                                {
                                        resources = resources ?? new Dictionary<string, object>();
                                        foreach (KeyValuePair<string, object> res in ve.Resources.MergedResources)
-                                               if (!resources.ContainsKey(res.Key))
-                                                       resources.Add(res.Key, res.Value);
+                                       {
+                                               // If a MergedDictionary value is overridden for a DynamicResource, 
+                                               // it comes out later in the enumeration of MergedResources
+                                               // TryGetValue ensures we pull the up-to-date value for the key
+                                               if (!resources.ContainsKey(res.Key) && ve.Resources.TryGetValue(res.Key, out object value))
+                                                       resources.Add(res.Key, value);
                                                else if (res.Key.StartsWith(Style.StyleClassPrefix, StringComparison.Ordinal))
                                                {
                                                        var mergedClassStyles = new List<Style>(resources[res.Key] as List<Style>);
                                                        mergedClassStyles.AddRange(res.Value as List<Style>);
                                                        resources[res.Key] = mergedClassStyles;
                                                }
+                                       }
                                }
                                var app = element as Application;
                                if (app != null && app.SystemResources != null)