2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using System.Globalization;
6 using System.Reflection;
8 using Tizen.NUI.Binding.Internals;
11 namespace Tizen.NUI.Binding
13 // [Xaml.ProvideCompiled("Xamarin.Forms.Core.XamlC.BindablePropertyConverter")]
14 [Xaml.TypeConversion(typeof(BindableProperty))]
15 internal sealed class BindablePropertyConverter : TypeConverter, IExtendedTypeConverter
17 object IExtendedTypeConverter.ConvertFrom(CultureInfo culture, object value, IServiceProvider serviceProvider)
19 return ((IExtendedTypeConverter)this).ConvertFromInvariantString(value as string, serviceProvider);
22 object IExtendedTypeConverter.ConvertFromInvariantString(string value, IServiceProvider serviceProvider)
24 if (string.IsNullOrWhiteSpace(value))
26 if (serviceProvider == null)
28 var parentValuesProvider = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideParentValues;
29 var typeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
30 if (typeResolver == null)
32 IXmlLineInfo lineinfo = null;
33 var xmlLineInfoProvider = serviceProvider.GetService(typeof(IXmlLineInfoProvider)) as IXmlLineInfoProvider;
34 if (xmlLineInfoProvider != null)
35 lineinfo = xmlLineInfoProvider.XmlLineInfo;
36 string[] parts = value.Split('.');
38 if (parts.Length == 1)
40 if (parentValuesProvider == null)
42 string msg = string.Format("Can't resolve {0}", parts[0]);
43 throw new XamlParseException(msg, lineinfo);
45 object parent = parentValuesProvider.ParentObjects.Skip(1).FirstOrDefault();
46 if (parentValuesProvider.TargetObject is Setter)
48 var style = parent as Style;
49 var triggerBase = parent as TriggerBase;
50 var visualState = parent as VisualState;
52 type = style.TargetType;
53 else if (triggerBase != null)
54 type = triggerBase.TargetType;
55 else if (visualState != null)
56 type = FindTypeForVisualState(parentValuesProvider, lineinfo);
58 else if (parentValuesProvider.TargetObject is Trigger)
59 type = (parentValuesProvider.TargetObject as Trigger).TargetType;
60 else if (parentValuesProvider.TargetObject is XamlPropertyCondition && (parent as TriggerBase) != null)
61 type = (parent as TriggerBase).TargetType;
64 throw new XamlParseException($"Can't resolve {parts [0]}", lineinfo);
66 return ConvertFrom(type, parts[0], lineinfo);
68 if (parts.Length == 2)
70 if (!typeResolver.TryResolve(parts[0], out type))
72 string msg = string.Format("Can't resolve {0}", parts[0]);
73 throw new XamlParseException(msg, lineinfo);
75 return ConvertFrom(type, parts[1], lineinfo);
77 throw new XamlParseException($"Can't resolve {value}. Syntax is [[prefix:]Type.]PropertyName.", lineinfo);
80 public override object ConvertFromInvariantString(string value)
82 if (string.IsNullOrWhiteSpace(value))
84 if (value.Contains(":"))
86 Console.WriteLine(null, "Can't resolve properties with xml namespace prefix.");
89 string[] parts = value.Split('.');
90 if (parts.Length != 2)
92 Console.WriteLine(null, $"Can't resolve {value}. Accepted syntax is Type.PropertyName.");
95 Type type = Type.GetType("Tizen.NUI." + parts[0]);
96 return ConvertFrom(type, parts[1], null);
99 BindableProperty ConvertFrom(Type type, string propertyName, IXmlLineInfo lineinfo)
101 string name = propertyName + "Property";
102 FieldInfo bpinfo = type.GetField(fi => fi.Name == name && fi.IsStatic && fi.IsPublic && fi.FieldType == typeof(BindableProperty));
104 throw new XamlParseException($"Can't resolve {name} on {type.Name}", lineinfo);
105 var bp = bpinfo.GetValue(null) as BindableProperty;
106 var isObsolete = bpinfo.GetCustomAttribute<ObsoleteAttribute>() != null;
107 if (bp != null && bp.PropertyName != propertyName && !isObsolete)
108 throw new XamlParseException($"The PropertyName of {type.Name}.{name} is not {propertyName}", lineinfo);
112 Type FindTypeForVisualState(IProvideParentValues parentValueProvider, IXmlLineInfo lineInfo)
114 var parents = parentValueProvider.ParentObjects.ToList();
116 // Skip 0; we would not be making this check if TargetObject were not a Setter
117 // Skip 1; we would not be making this check if the immediate parent were not a VisualState
119 // VisualStates must be in a VisualStateGroup
120 if(!(parents[2] is VisualStateGroup)) {
121 throw new XamlParseException($"Expected {nameof(VisualStateGroup)} but found {parents[2]}.", lineInfo);
124 var vsTarget = parents[3];
126 // Are these Visual States directly on a VisualElement?
127 if (vsTarget is /*VisualElement*/BaseHandle)
129 return vsTarget.GetType();
132 if (!(parents[3] is VisualStateGroupList))
134 throw new XamlParseException($"Expected {nameof(VisualStateGroupList)} but found {parents[3]}.", lineInfo);
137 if (!(parents[4] is Setter))
139 throw new XamlParseException($"Expected {nameof(Setter)} but found {parents[4]}.", lineInfo);
142 // These must be part of a Style; verify that
143 if (!(parents[5] is Style style))
145 throw new XamlParseException($"Expected {nameof(Style)} but found {parents[5]}.", lineInfo);
148 return style.TargetType;