diff options
author | Stephane Delcroix <stephane@delcroix.org> | 2017-04-11 20:09:06 +0200 |
---|---|---|
committer | Jason Smith <jason.smith@xamarin.com> | 2017-04-11 11:09:06 -0700 |
commit | d3cc1b5651918c9d306ec0002515371cae85262a (patch) | |
tree | 9c77998e3a5849e27c36da703e79ccdfe615ec5c | |
parent | 093a4efa2da9cb681890158ca8ee9bb6f30b0a8c (diff) | |
download | xamarin-forms-d3cc1b5651918c9d306ec0002515371cae85262a.tar.gz xamarin-forms-d3cc1b5651918c9d306ec0002515371cae85262a.tar.bz2 xamarin-forms-d3cc1b5651918c9d306ec0002515371cae85262a.zip |
[C] Lookup in RD also lookup in merged RDs (#861)
-rw-r--r-- | Xamarin.Forms.Core.UnitTests/ResourceDictionaryTests.cs | 53 | ||||
-rw-r--r-- | Xamarin.Forms.Core/ResourceDictionary.cs | 10 | ||||
-rw-r--r-- | Xamarin.Forms.Xaml/MarkupExtensions/StaticResourceExtension.cs | 4 |
3 files changed, 58 insertions, 9 deletions
diff --git a/Xamarin.Forms.Core.UnitTests/ResourceDictionaryTests.cs b/Xamarin.Forms.Core.UnitTests/ResourceDictionaryTests.cs index 3621d955..d1a43732 100644 --- a/Xamarin.Forms.Core.UnitTests/ResourceDictionaryTests.cs +++ b/Xamarin.Forms.Core.UnitTests/ResourceDictionaryTests.cs @@ -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 diff --git a/Xamarin.Forms.Core/ResourceDictionary.cs b/Xamarin.Forms.Core/ResourceDictionary.cs index c9413eb3..2fc11ffc 100644 --- a/Xamarin.Forms.Core/ResourceDictionary.cs +++ b/Xamarin.Forms.Core/ResourceDictionary.cs @@ -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)); } diff --git a/Xamarin.Forms.Xaml/MarkupExtensions/StaticResourceExtension.cs b/Xamarin.Forms.Xaml/MarkupExtensions/StaticResourceExtension.cs index 16c80533..14e0b8be 100644 --- a/Xamarin.Forms.Xaml/MarkupExtensions/StaticResourceExtension.cs +++ b/Xamarin.Forms.Xaml/MarkupExtensions/StaticResourceExtension.cs @@ -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; } |