3 using System.Reflection;
5 using Tizen.NUI.Binding;
6 using System.ComponentModel;
8 namespace Tizen.NUI.Xaml
10 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
11 [EditorBrowsable(EditorBrowsableState.Never)]
12 [ContentProperty("Key")]
13 public sealed class StaticResourceExtension : IMarkupExtension
15 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
16 [EditorBrowsable(EditorBrowsableState.Never)]
17 public string Key { get; set; }
19 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
20 [EditorBrowsable(EditorBrowsableState.Never)]
21 public object ProvideValue(IServiceProvider serviceProvider)
23 if (serviceProvider == null)
24 throw new ArgumentNullException(nameof(serviceProvider));
27 var lineInfoProvider = serviceProvider.GetService(typeof(IXmlLineInfoProvider)) as IXmlLineInfoProvider;
28 var lineInfo = (lineInfoProvider != null) ? lineInfoProvider.XmlLineInfo : new XmlLineInfo();
29 throw new XamlParseException("you must specify a key in {StaticResource}", lineInfo);
31 var valueProvider = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideParentValues;
32 if (valueProvider == null)
33 throw new ArgumentException(nameof(valueProvider));
34 var xmlLineInfoProvider = serviceProvider.GetService(typeof(IXmlLineInfoProvider)) as IXmlLineInfoProvider;
35 var xmlLineInfo = xmlLineInfoProvider != null ? xmlLineInfoProvider.XmlLineInfo : null;
36 object resource = null;
38 foreach (var p in valueProvider.ParentObjects)
40 var irp = p as IResourcesProvider;
41 var resDict = irp != null && irp.IsResourcesCreated ? irp.XamlResources : p as ResourceDictionary;
44 if (resDict.TryGetValue(Key, out resource))
47 resource = resource ?? GetApplicationLevelResource(Key, xmlLineInfo);
49 var bp = valueProvider.TargetProperty as BindableProperty;
50 var pi = valueProvider.TargetProperty as PropertyInfo;
51 var propertyType = bp?.ReturnType ?? pi?.PropertyType;
52 if (propertyType == null)
56 if (resource.GetType().GetTypeInfo().IsGenericType && (resource.GetType().GetGenericTypeDefinition() == typeof(OnPlatform<>) || resource.GetType().GetGenericTypeDefinition() == typeof(OnIdiom<>)))
58 // This is only there to support our backward compat story with pre 2.3.3 compiled Xaml project who was not providing TargetProperty
59 var method = resource.GetType().GetRuntimeMethod("op_Implicit", new[] { resource.GetType() });
62 resource = method.Invoke(null, new[] { resource });
68 if (propertyType.IsAssignableFrom(resource?.GetType()))
70 var implicit_op = resource?.GetType().GetImplicitConversionOperator(fromType: resource?.GetType(), toType: propertyType)
71 ?? propertyType.GetImplicitConversionOperator(fromType: resource?.GetType(), toType: propertyType);
72 if (implicit_op != null)
73 return implicit_op.Invoke(resource, new[] { resource });
77 //Special case for https://bugzilla.xamarin.com/show_bug.cgi?id=59818
78 //On OnPlatform, check for an opImplicit from the targetType
79 if (Device.Flags != null
80 && Device.Flags.Contains("xamlDoubleImplicitOpHack")
81 && resource.GetType().GetTypeInfo().IsGenericType
82 && (resource.GetType().GetGenericTypeDefinition() == typeof(OnPlatform<>)))
84 var tType = resource.GetType().GenericTypeArguments[0];
85 var opImplicit = tType.GetImplicitConversionOperator(fromType: tType, toType: propertyType)
86 ?? propertyType.GetImplicitConversionOperator(fromType: tType, toType: propertyType);
88 if (opImplicit != null)
90 //convert the OnPlatform<T> to T
91 var opPlatformImplicitConversionOperator = resource?.GetType().GetImplicitConversionOperator(fromType: resource?.GetType(), toType: tType);
92 resource = opPlatformImplicitConversionOperator?.Invoke(null, new[] { resource });
94 //and convert to toType
95 resource = opImplicit.Invoke(null, new[] { resource });
103 internal object GetApplicationLevelResource(string key, IXmlLineInfo xmlLineInfo)
105 object resource = null;
106 if (Application.Current == null || !((IResourcesProvider)Application.Current).IsResourcesCreated || !Application.Current.XamlResources.TryGetValue(Key, out resource))
107 throw new XamlParseException($"StaticResource not found for key {Key}", xmlLineInfo);