summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Xaml
diff options
context:
space:
mode:
authorE.Z. Hart <hartez@users.noreply.github.com>2016-04-07 00:12:07 -0600
committerJason Smith <jason.smith@xamarin.com>2016-04-06 23:12:07 -0700
commitc98d338e15a29897e3a9a9d03d30dd3c70347925 (patch)
tree151819a41191d12609a5455c48e84be0b3879452 /Xamarin.Forms.Xaml
parent1363f383b17d68fbafadc773b9f06f477f9744c9 (diff)
downloadxamarin-forms-c98d338e15a29897e3a9a9d03d30dd3c70347925.tar.gz
xamarin-forms-c98d338e15a29897e3a9a9d03d30dd3c70347925.tar.bz2
xamarin-forms-c98d338e15a29897e3a9a9d03d30dd3c70347925.zip
Force conversion to target type when Static Resource returns OnPlatform<T>
Diffstat (limited to 'Xamarin.Forms.Xaml')
-rw-r--r--Xamarin.Forms.Xaml/MarkupExtensions/StaticResourceExtension.cs29
1 files changed, 26 insertions, 3 deletions
diff --git a/Xamarin.Forms.Xaml/MarkupExtensions/StaticResourceExtension.cs b/Xamarin.Forms.Xaml/MarkupExtensions/StaticResourceExtension.cs
index e5a7aef7..4f1465d3 100644
--- a/Xamarin.Forms.Xaml/MarkupExtensions/StaticResourceExtension.cs
+++ b/Xamarin.Forms.Xaml/MarkupExtensions/StaticResourceExtension.cs
@@ -1,4 +1,5 @@
using System;
+using System.Reflection;
namespace Xamarin.Forms.Xaml
{
@@ -32,13 +33,35 @@ namespace Xamarin.Forms.Xaml
continue;
object res;
if (ve.Resources.TryGetValue(Key, out res))
- return res;
+ {
+ return ConvertCompiledOnPlatform(res);
+ }
}
if (Application.Current != null && Application.Current.Resources != null &&
Application.Current.Resources.ContainsKey(Key))
- return Application.Current.Resources[Key];
+ {
+ var resource = Application.Current.Resources[Key];
+
+ return ConvertCompiledOnPlatform(resource);
+ }
+
+ throw new XamlParseException($"StaticResource not found for key {Key}", xmlLineInfo);
+ }
+
+ static object ConvertCompiledOnPlatform(object resource)
+ {
+ var actualType = resource.GetType();
+ if (actualType.GetTypeInfo().IsGenericType && actualType.GetGenericTypeDefinition() == typeof(OnPlatform<>))
+ {
+ // If we're accessing OnPlatform via a StaticResource in compiled XAML
+ // we'll have to handle the cast to the target type manually
+ // (Normally the compiled XAML handles this by calling `implicit` explicitly,
+ // but it doesn't know to do that when it's using a static resource)
+ var method = actualType.GetRuntimeMethod("op_Implicit", new[] { actualType });
+ resource = method.Invoke(resource, new[] { resource });
+ }
- throw new XamlParseException(string.Format("StaticResource not found for key {0}", Key), xmlLineInfo);
+ return resource;
}
}
} \ No newline at end of file