[C] Lookup in RD also lookup in merged RDs (#861)
authorStephane Delcroix <stephane@delcroix.org>
Tue, 11 Apr 2017 18:09:06 +0000 (20:09 +0200)
committerJason Smith <jason.smith@xamarin.com>
Tue, 11 Apr 2017 18:09:06 +0000 (11:09 -0700)
Xamarin.Forms.Core.UnitTests/ResourceDictionaryTests.cs
Xamarin.Forms.Core/ResourceDictionary.cs
Xamarin.Forms.Xaml/MarkupExtensions/StaticResourceExtension.cs

index 3621d95..d1a4373 100644 (file)
@@ -290,7 +290,7 @@ namespace Xamarin.Forms.Core.UnitTests
                        rd0.MergedWith = typeof(MyRD);
 
                        object _;
-                       Assert.True(rd0.TryGetMergedValue("foo", out _));
+                       Assert.True(rd0.TryGetValue("foo", out _));
                        Assert.AreEqual("Foo", _);
                }
 
@@ -307,5 +307,56 @@ namespace Xamarin.Forms.Core.UnitTests
                        }
                        Assert.Fail();
                }
+
+               [Test]
+               public void ContainsReturnsValuesForMergedRD()
+               {
+                       var rd = new ResourceDictionary {
+                               {"baz", "BAZ"},
+                               {"qux", "QUX"},
+                       };
+                       rd.MergedWith = typeof(MyRD);
+
+                       Assert.That(rd.Contains(new KeyValuePair<string, object>("foo", "Foo")), Is.True);
+               }
+
+               [Test]
+               public void CountDoesNotIncludeMerged()
+               {
+                       var rd = new ResourceDictionary {
+                               {"baz", "Baz"},
+                               {"qux", "Qux"},
+                       };
+                       rd.MergedWith = typeof(MyRD);
+
+                       Assert.That(rd.Count, Is.EqualTo(2));
+               }
+
+               [Test]
+               public void IndexerLookupInMerged()
+               {
+                       var rd = new ResourceDictionary {
+                               {"baz", "BAZ"},
+                               {"qux", "QUX"},
+                       };
+                       rd.MergedWith = typeof(MyRD);
+
+                       Assert.That(() => rd["foo"], Throws.Nothing);
+                       Assert.That(rd["foo"], Is.EqualTo("Foo"));
+               }
+
+               [Test]
+               public void TryGetValueLookupInMerged()
+               {
+                       var rd = new ResourceDictionary { 
+                               {"baz", "BAZ"},
+                               {"qux", "QUX"},
+                       };
+                       rd.MergedWith = typeof(MyRD);
+
+                       object _;
+                       Assert.That(rd.TryGetValue("foo", out _), Is.True);
+                       Assert.That(rd.TryGetValue("baz", out _), Is.True);
+}
        }
 }
\ No newline at end of file
index c9413eb..2fc11ff 100644 (file)
@@ -48,7 +48,8 @@ namespace Xamarin.Forms
 
                bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item)
                {
-                       return ((ICollection<KeyValuePair<string, object>>)_innerDictionary).Contains(item);
+                       return ((ICollection<KeyValuePair<string, object>>)_innerDictionary).Contains(item)
+                               || (_mergedInstance != null && _mergedInstance.Contains(item));
                }
 
                void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
@@ -91,6 +92,8 @@ namespace Xamarin.Forms
                        {
                                if (_innerDictionary.ContainsKey(index))
                                        return _innerDictionary[index];
+                               if (_mergedInstance != null && _mergedInstance.ContainsKey(index))
+                                       return _mergedInstance[index];
                                throw new KeyNotFoundException($"The resource '{index}' is not present in the dictionary.");
                        }
                        set
@@ -137,11 +140,6 @@ namespace Xamarin.Forms
 
                public bool TryGetValue(string key, out object value)
                {
-                       return _innerDictionary.TryGetValue(key, out value);
-               }
-
-               internal bool TryGetMergedValue(string key, out object value)
-               {
                        return _innerDictionary.TryGetValue(key, out value) || (_mergedInstance != null && _mergedInstance.TryGetValue(key, out value));
                }
 
index 16c8053..14e0b8b 100644 (file)
@@ -31,7 +31,7 @@ namespace Xamarin.Forms.Xaml
                                var resDict = ve?.Resources ?? p as ResourceDictionary;
                                if (resDict == null)
                                        continue;
-                               if (resDict.TryGetMergedValue(Key, out resource))
+                               if (resDict.TryGetValue(Key, out resource))
                                        break;
                        }
                        resource = resource ?? GetApplicationLevelResource(Key, xmlLineInfo);
@@ -59,7 +59,7 @@ namespace Xamarin.Forms.Xaml
                internal object GetApplicationLevelResource(string key, IXmlLineInfo xmlLineInfo)
                {
                        object resource;
-                       if (Application.Current == null || Application.Current.Resources == null || !Application.Current.Resources.TryGetMergedValue(Key, out resource))
+                       if (Application.Current == null || Application.Current.Resources == null || !Application.Current.Resources.TryGetValue(Key, out resource))
                                throw new XamlParseException($"StaticResource not found for key {Key}", xmlLineInfo);
                        return resource;
                }