3 using System.Reflection;
5 using Tizen.NUI.Binding;
8 namespace Tizen.NUI.Xaml
10 [ContentProperty("Key")]
11 internal sealed class StaticResourceExtension : IMarkupExtension
13 public string Key { get; set; }
15 public object ProvideValue(IServiceProvider serviceProvider)
17 if (serviceProvider == null)
18 throw new ArgumentNullException(nameof(serviceProvider));
20 var lineInfoProvider = serviceProvider.GetService(typeof(IXmlLineInfoProvider)) as IXmlLineInfoProvider;
21 var lineInfo = (lineInfoProvider != null) ? lineInfoProvider.XmlLineInfo : new XmlLineInfo();
22 throw new XamlParseException("you must specify a key in {StaticResource}", lineInfo);
24 var valueProvider = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideParentValues;
25 if (valueProvider == null)
26 throw new ArgumentException();
27 var xmlLineInfoProvider = serviceProvider.GetService(typeof(IXmlLineInfoProvider)) as IXmlLineInfoProvider;
28 var xmlLineInfo = xmlLineInfoProvider != null ? xmlLineInfoProvider.XmlLineInfo : null;
29 object resource = null;
31 foreach (var p in valueProvider.ParentObjects) {
32 var irp = p as IResourcesProvider;
33 var resDict = irp != null && irp.IsResourcesCreated ? irp.XamlResources : p as ResourceDictionary;
36 if (resDict.TryGetValue(Key, out resource))
39 resource = resource ?? GetApplicationLevelResource(Key, xmlLineInfo);
41 var bp = valueProvider.TargetProperty as BindableProperty;
42 var pi = valueProvider.TargetProperty as PropertyInfo;
43 var propertyType = bp?.ReturnType ?? pi?.PropertyType;
44 if (propertyType == null) {
45 if (resource != null) {
46 if (resource.GetType().GetTypeInfo().IsGenericType && (resource.GetType().GetGenericTypeDefinition() == typeof(OnPlatform<>) || resource.GetType().GetGenericTypeDefinition() == typeof(OnIdiom<>))) {
47 // This is only there to support our backward compat story with pre 2.3.3 compiled Xaml project who was not providing TargetProperty
48 var method = resource.GetType().GetRuntimeMethod("op_Implicit", new[] { resource.GetType() });
49 resource = method.Invoke(null, new[] { resource });
54 if (propertyType.IsAssignableFrom(resource?.GetType()))
56 var implicit_op = resource?.GetType().GetImplicitConversionOperator(fromType: resource?.GetType(), toType: propertyType)
57 ?? propertyType.GetImplicitConversionOperator(fromType: resource?.GetType(), toType: propertyType);
58 if (implicit_op != null)
59 return implicit_op.Invoke(resource, new [] { resource });
61 if (resource != null) {
62 //Special case for https://bugzilla.xamarin.com/show_bug.cgi?id=59818
63 //On OnPlatform, check for an opImplicit from the targetType
64 if ( Device.Flags != null
65 && Device.Flags.Contains("xamlDoubleImplicitOpHack")
66 && resource.GetType().GetTypeInfo().IsGenericType
67 && (resource.GetType().GetGenericTypeDefinition() == typeof(OnPlatform<>))) {
68 var tType = resource.GetType().GenericTypeArguments[0];
69 var opImplicit = tType.GetImplicitConversionOperator(fromType: tType, toType: propertyType)
70 ?? propertyType.GetImplicitConversionOperator(fromType: tType, toType: propertyType);
72 if (opImplicit != null) {
73 //convert the OnPlatform<T> to T
74 var opPlatformImplicitConversionOperator = resource?.GetType().GetImplicitConversionOperator(fromType: resource?.GetType(), toType: tType);
75 resource = opPlatformImplicitConversionOperator?.Invoke(null, new[] { resource });
77 //and convert to toType
78 resource = opImplicit.Invoke(null, new[] { resource });
86 internal object GetApplicationLevelResource(string key, IXmlLineInfo xmlLineInfo)
88 object resource = null;
89 if (Application.Current == null || !((IResourcesProvider)Application.Current).IsResourcesCreated || !Application.Current.XamlResources.TryGetValue(Key, out resource))
90 throw new XamlParseException($"StaticResource not found for key {Key}", xmlLineInfo);