summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Xaml/MarkupExtensions/StaticResourceExtension.cs
diff options
context:
space:
mode:
authorJason Smith <jason.smith@xamarin.com>2016-03-22 20:02:25 (GMT)
committerJason Smith <jason.smith@xamarin.com>2016-03-22 23:13:41 (GMT)
commit17fdde66d94155fc62a034fa6658995bef6fd6e5 (patch)
treeb5e5073a2a7b15cdbe826faa5c763e270a505729 /Xamarin.Forms.Xaml/MarkupExtensions/StaticResourceExtension.cs
downloadxamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.zip
xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.gz
xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.bz2
Initial import
Diffstat (limited to 'Xamarin.Forms.Xaml/MarkupExtensions/StaticResourceExtension.cs')
-rw-r--r--Xamarin.Forms.Xaml/MarkupExtensions/StaticResourceExtension.cs44
1 files changed, 44 insertions, 0 deletions
diff --git a/Xamarin.Forms.Xaml/MarkupExtensions/StaticResourceExtension.cs b/Xamarin.Forms.Xaml/MarkupExtensions/StaticResourceExtension.cs
new file mode 100644
index 0000000..e5a7aef
--- /dev/null
+++ b/Xamarin.Forms.Xaml/MarkupExtensions/StaticResourceExtension.cs
@@ -0,0 +1,44 @@
+using System;
+
+namespace Xamarin.Forms.Xaml
+{
+ [ContentProperty("Key")]
+ public sealed class StaticResourceExtension : IMarkupExtension
+ {
+ public string Key { get; set; }
+
+ public object ProvideValue(IServiceProvider serviceProvider)
+ {
+ if (serviceProvider == null)
+ throw new ArgumentNullException("serviceProvider");
+ if (Key == null)
+ {
+ var lineInfoProvider = serviceProvider.GetService(typeof (IXmlLineInfoProvider)) as IXmlLineInfoProvider;
+ var lineInfo = (lineInfoProvider != null) ? lineInfoProvider.XmlLineInfo : new XmlLineInfo();
+ throw new XamlParseException("you must specify a key in {StaticResource}", lineInfo);
+ }
+ var valueProvider = serviceProvider.GetService(typeof (IProvideValueTarget)) as IProvideParentValues;
+ if (valueProvider == null)
+ throw new ArgumentException();
+ var xmlLineInfoProvider = serviceProvider.GetService(typeof (IXmlLineInfoProvider)) as IXmlLineInfoProvider;
+ var xmlLineInfo = xmlLineInfoProvider != null ? xmlLineInfoProvider.XmlLineInfo : null;
+
+ foreach (var p in valueProvider.ParentObjects)
+ {
+ var ve = p as VisualElement;
+ if (ve == null)
+ continue;
+ if (ve.Resources == null)
+ continue;
+ object res;
+ if (ve.Resources.TryGetValue(Key, out res))
+ return res;
+ }
+ if (Application.Current != null && Application.Current.Resources != null &&
+ Application.Current.Resources.ContainsKey(Key))
+ return Application.Current.Resources[Key];
+
+ throw new XamlParseException(string.Format("StaticResource not found for key {0}", Key), xmlLineInfo);
+ }
+ }
+} \ No newline at end of file