summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Xaml
diff options
context:
space:
mode:
authorStephane Delcroix <stephane@delcroix.org>2017-04-07 07:48:17 (GMT)
committerGitHub <noreply@github.com>2017-04-07 07:48:17 (GMT)
commit5ea000a76d03a5328fe18761fbdf8b05e26a170b (patch)
tree128b2a0f74276be3081c3cada0716ad9bf64fa46 /Xamarin.Forms.Xaml
parent5f2796e160f99bad31c1b06a2d1f328b5ed02ba3 (diff)
downloadxamarin-forms-5ea000a76d03a5328fe18761fbdf8b05e26a170b.zip
xamarin-forms-5ea000a76d03a5328fe18761fbdf8b05e26a170b.tar.gz
xamarin-forms-5ea000a76d03a5328fe18761fbdf8b05e26a170b.tar.bz2
[Xaml] Set the TargetProperty on ServiceProvider (#847)
Diffstat (limited to 'Xamarin.Forms.Xaml')
-rw-r--r--Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs67
-rw-r--r--Xamarin.Forms.Xaml/XamlServiceProvider.cs2
2 files changed, 53 insertions, 16 deletions
diff --git a/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs b/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs
index 337b2ac..af7acaf 100644
--- a/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs
+++ b/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs
@@ -77,7 +77,8 @@ namespace Xamarin.Forms.Xaml
public void Visit(ElementNode node, INode parentNode)
{
var propertyName = XmlName.Empty;
- if (TryGetPropertyName(node, parentNode, out propertyName) && propertyName == XmlName._CreateContent){
+
+ if (TryGetPropertyName(node, parentNode, out propertyName) && propertyName == XmlName._CreateContent) {
var s0 = Values[parentNode];
if (s0 is ElementTemplate) {
SetTemplate(s0 as ElementTemplate, node);
@@ -85,21 +86,7 @@ namespace Xamarin.Forms.Xaml
}
}
- var value = Values [node];
var parentElement = parentNode as IElementNode;
- var markupExtension = value as IMarkupExtension;
- var valueProvider = value as IValueProvider;
-
- if (markupExtension != null) {
- var serviceProvider = value.GetType().GetTypeInfo().GetCustomAttribute<AcceptEmptyServiceProviderAttribute>() == null ? new XamlServiceProvider(node, Context) : null;
- value = markupExtension.ProvideValue(serviceProvider);
- }
-
- if (valueProvider != null) {
- var serviceProvider = value.GetType().GetTypeInfo().GetCustomAttribute<AcceptEmptyServiceProviderAttribute>() == null ? new XamlServiceProvider(node, Context) : null;
- value = valueProvider.ProvideValue(serviceProvider);
- }
-
propertyName = XmlName.Empty;
//Simplify ListNodes with single elements
@@ -110,6 +97,15 @@ namespace Xamarin.Forms.Xaml
parentElement = parentNode as IElementNode;
}
+ var value = Values[node];
+
+ var markupExtension = value as IMarkupExtension;
+ var valueProvider = value as IValueProvider;
+ XamlServiceProvider serviceProvider = null;
+ if (markupExtension != null || valueProvider != null)
+ serviceProvider = value.GetType().GetTypeInfo().GetCustomAttribute<AcceptEmptyServiceProviderAttribute>() == null ? new XamlServiceProvider(node, Context) : null;
+
+
if (propertyName != XmlName.Empty || TryGetPropertyName(node, parentNode, out propertyName)) {
if (Skips.Contains(propertyName))
return;
@@ -117,8 +113,21 @@ namespace Xamarin.Forms.Xaml
return;
var source = Values [parentNode];
+ if (serviceProvider != null)
+ ((XamlValueTargetProvider)serviceProvider.IProvideValueTarget).TargetProperty = GetTargetProperty(source, propertyName, Context, node);
+
+ if (markupExtension != null)
+ value = markupExtension.ProvideValue(serviceProvider);
+ else if (valueProvider != null)
+ value = valueProvider.ProvideValue(serviceProvider);
+
SetPropertyValue(source, propertyName, value, Context.RootElement, node, Context, node);
} else if (IsCollectionItem(node, parentNode) && parentNode is IElementNode) {
+ if (markupExtension != null)
+ value = markupExtension.ProvideValue(serviceProvider);
+ else if (valueProvider != null)
+ value = valueProvider.ProvideValue(serviceProvider);
+
// Collection element, implicit content, or implicit collection element.
string contentProperty;
if (typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(Context.Types [parentElement].GetTypeInfo()) && Context.Types[parentElement].GetRuntimeMethods().Any(mi => mi.Name == "Add" && mi.GetParameters().Length == 1)) {
@@ -140,6 +149,11 @@ namespace Xamarin.Forms.Xaml
} else
throw new XamlParseException($"Can not set the content of {((IElementNode)parentNode).XmlType.Name} as it doesn't have a ContentPropertyAttribute", node);
} else if (IsCollectionItem(node, parentNode) && parentNode is ListNode) {
+ if (markupExtension != null)
+ value = markupExtension.ProvideValue(serviceProvider);
+ else if (valueProvider != null)
+ value = valueProvider.ProvideValue(serviceProvider);
+
var parentList = (ListNode)parentNode;
var source = Values [parentNode.Parent];
@@ -269,6 +283,22 @@ namespace Xamarin.Forms.Xaml
return null;
}
+ static object GetTargetProperty(object xamlelement, XmlName propertyName, HydratationContext context, IXmlLineInfo lineInfo)
+ {
+ var localName = propertyName.LocalName;
+ //If it's an attached BP, update elementType and propertyName
+ var bpOwnerType = xamlelement.GetType();
+ GetRealNameAndType(ref bpOwnerType, propertyName.NamespaceURI, ref localName, context, lineInfo);
+ var property = GetBindableProperty(bpOwnerType, localName, lineInfo, false);
+
+ if (property != null)
+ return property;
+
+ var elementType = xamlelement.GetType();
+ var propertyInfo = elementType.GetRuntimeProperties().FirstOrDefault(p => p.Name == localName);
+ return propertyInfo;
+ }
+
public static void SetPropertyValue(object xamlelement, XmlName propertyName, object value, object rootElement, INode node, HydratationContext context, IXmlLineInfo lineInfo)
{
var localName = propertyName.LocalName;
@@ -397,6 +427,9 @@ namespace Xamarin.Forms.Xaml
if (property == null)
return false;
+ if (serviceProvider != null && serviceProvider.IProvideValueTarget != null)
+ ((XamlValueTargetProvider)serviceProvider.IProvideValueTarget).TargetProperty = property;
+
Func<MemberInfo> minforetriever;
if (attached)
minforetriever = () => property.DeclaringType.GetRuntimeMethod("Get" + property.PropertyName, new [] { typeof(BindableObject) });
@@ -436,6 +469,9 @@ namespace Xamarin.Forms.Xaml
if (!IsVisibleFrom(setter, context.RootElement))
return false;
+ if (serviceProvider != null && serviceProvider.IProvideValueTarget != null)
+ ((XamlValueTargetProvider)serviceProvider.IProvideValueTarget).TargetProperty = propertyInfo;
+
object convertedValue = value.ConvertTo(propertyInfo.PropertyType, () => propertyInfo, serviceProvider);
if (convertedValue != null && !propertyInfo.PropertyType.IsInstanceOfType(convertedValue))
return false;
@@ -475,6 +511,7 @@ namespace Xamarin.Forms.Xaml
if (addMethod == null)
return false;
+ ((XamlValueTargetProvider)serviceProvider.IProvideValueTarget).TargetProperty = propertyInfo;
addMethod.Invoke(collection, new [] { value.ConvertTo(addMethod.GetParameters() [0].ParameterType, (Func<TypeConverter>)null, serviceProvider) });
return true;
}
diff --git a/Xamarin.Forms.Xaml/XamlServiceProvider.cs b/Xamarin.Forms.Xaml/XamlServiceProvider.cs
index 5d99f95..674795f 100644
--- a/Xamarin.Forms.Xaml/XamlServiceProvider.cs
+++ b/Xamarin.Forms.Xaml/XamlServiceProvider.cs
@@ -103,7 +103,7 @@ namespace Xamarin.Forms.Xaml.Internals
HydratationContext Context { get; }
public object TargetObject { get; }
- public object TargetProperty { get; } = null;
+ public object TargetProperty { get; internal set; } = null;
IEnumerable<object> IProvideParentValues.ParentObjects
{