summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephane Delcroix <stephane@delcroix.org>2016-12-01 22:10:11 +0100
committerStephane Delcroix <stephane@delcroix.org>2016-12-01 23:06:14 +0100
commit3c6988b13ae804683c34f37bfb7c88261101f8d6 (patch)
treed03b8257921448d89afb8a0daadbb4bc45017c9f
parent121ddafde56c66f0b720e2cb27c2f3aed13027f2 (diff)
downloadxamarin-forms-3c6988b13ae804683c34f37bfb7c88261101f8d6.tar.gz
xamarin-forms-3c6988b13ae804683c34f37bfb7c88261101f8d6.tar.bz2
xamarin-forms-3c6988b13ae804683c34f37bfb7c88261101f8d6.zip
[XamlC] support non-generic IMarkup on ABPs (#562)
* [XamlC] support custom markups on ABPs * [XamlC] test for 47950
-rw-r--r--Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs18
-rw-r--r--Xamarin.Forms.Xaml.UnitTests/Issues/Bz47950.xaml12
-rw-r--r--Xamarin.Forms.Xaml.UnitTests/Issues/Bz47950.xaml.cs40
-rw-r--r--Xamarin.Forms.Xaml.UnitTests/Issues/Unreported004.xaml.cs2
-rw-r--r--Xamarin.Forms.Xaml.UnitTests/Issues/Unreported005.xaml12
-rw-r--r--Xamarin.Forms.Xaml.UnitTests/Issues/Unreported005.xaml.cs78
-rw-r--r--Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj12
7 files changed, 167 insertions, 7 deletions
diff --git a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs
index f481cbee..4523a49d 100644
--- a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs
+++ b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs
@@ -92,9 +92,10 @@ namespace Xamarin.Forms.Build.Tasks
var localName = propertyName.LocalName;
TypeReference declaringTypeReference = null;
FieldReference bpRef = null;
+ var _ = false;
PropertyDefinition propertyRef = null;
if (parentNode is IElementNode && propertyName != XmlName.Empty) {
- bpRef = GetBindablePropertyReference(Context.Variables [(IElementNode)parentNode], propertyName.NamespaceURI, ref localName, Context, node);
+ bpRef = GetBindablePropertyReference(Context.Variables [(IElementNode)parentNode], propertyName.NamespaceURI, ref localName, out _, Context, node);
propertyRef = Context.Variables [(IElementNode)parentNode].VariableType.GetProperty(pd => pd.Name == localName, out declaringTypeReference);
}
Context.IL.Append(ProvideValue(vardefref, Context, Module, node, bpRef:bpRef, propertyRef:propertyRef, propertyDeclaringTypeRef: declaringTypeReference));
@@ -316,7 +317,8 @@ namespace Xamarin.Forms.Build.Tasks
{
var module = context.Body.Method.Module;
var localName = propertyName.LocalName;
- var bpRef = GetBindablePropertyReference(parent, propertyName.NamespaceURI, ref localName, context, iXmlLineInfo);
+ bool attached;
+ var bpRef = GetBindablePropertyReference(parent, propertyName.NamespaceURI, ref localName, out attached, context, iXmlLineInfo);
//If the target is an event, connect
if (CanConnectEvent(parent, localName))
@@ -331,7 +333,7 @@ namespace Xamarin.Forms.Build.Tasks
return SetBinding(parent, bpRef, valueNode as IElementNode, iXmlLineInfo, context);
//If it's a BP, SetValue ()
- if (CanSetValue(bpRef, valueNode, iXmlLineInfo, context))
+ if (CanSetValue(bpRef, attached, valueNode, iXmlLineInfo, context))
return SetValue(parent, bpRef, valueNode, iXmlLineInfo, context);
//If it's a property, set it
@@ -345,14 +347,14 @@ namespace Xamarin.Forms.Build.Tasks
throw new XamlParseException($"No property, bindable property, or event found for '{localName}'", iXmlLineInfo);
}
- static FieldReference GetBindablePropertyReference(VariableDefinition parent, string namespaceURI, ref string localName, ILContext context, IXmlLineInfo iXmlLineInfo)
+ static FieldReference GetBindablePropertyReference(VariableDefinition parent, string namespaceURI, ref string localName, out bool attached, ILContext context, IXmlLineInfo iXmlLineInfo)
{
var module = context.Body.Method.Module;
TypeReference declaringTypeReference;
//If it's an attached BP, update elementType and propertyName
var bpOwnerType = parent.VariableType;
- GetNameAndTypeRef(ref bpOwnerType, namespaceURI, ref localName, context, iXmlLineInfo);
+ attached = GetNameAndTypeRef(ref bpOwnerType, namespaceURI, ref localName, context, iXmlLineInfo);
var name = $"{localName}Property";
FieldReference bpRef = bpOwnerType.GetField(fd => fd.Name == name &&
fd.IsStatic &&
@@ -473,7 +475,7 @@ namespace Xamarin.Forms.Build.Tasks
yield return Instruction.Create(OpCodes.Callvirt, module.Import(setBinding));
}
- static bool CanSetValue(FieldReference bpRef, INode node, IXmlLineInfo iXmlLineInfo, ILContext context)
+ static bool CanSetValue(FieldReference bpRef, bool attached, INode node, IXmlLineInfo iXmlLineInfo, ILContext context)
{
var module = context.Body.Method.Module;
@@ -492,6 +494,10 @@ namespace Xamarin.Forms.Build.Tasks
return false;
var bpTypeRef = bpRef.GetBindablePropertyType(iXmlLineInfo, module);
+ // If it's an attached BP, there's no second chance to handle IMarkupExtensions, so we try here.
+ // Worst case scenario ? InvalidCastException at runtime
+ if (attached && varValue.VariableType.FullName == "System.Object")
+ return true;
return varValue.VariableType.InheritsFromOrImplements(bpTypeRef);
}
diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz47950.xaml b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz47950.xaml
new file mode 100644
index 00000000..22120971
--- /dev/null
+++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz47950.xaml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
+ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
+ xmlns:local="clr-namespace:Xamarin.Forms.Xaml.UnitTests"
+ x:Class="Xamarin.Forms.Xaml.UnitTests.Bz47950">
+ <ContentPage.Resources>
+ <ResourceDictionary>
+ <Color x:Key="MyColor">#c2d1d3</Color>
+ </ResourceDictionary>
+ </ContentPage.Resources>
+ <Label x:Name="label" local:Bz47950Behavior.ColorTest="{StaticResource MyColor}" />
+</ContentPage> \ No newline at end of file
diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz47950.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz47950.xaml.cs
new file mode 100644
index 00000000..738e1cee
--- /dev/null
+++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz47950.xaml.cs
@@ -0,0 +1,40 @@
+´╗┐using System;
+using System.Collections.Generic;
+using NUnit.Framework;
+using Xamarin.Forms;
+
+namespace Xamarin.Forms.Xaml.UnitTests
+{
+ public class Bz47950Behavior : Behavior<View>
+ {
+ public static readonly BindableProperty ColorTestProperty =
+ BindableProperty.CreateAttached("ColorTest", typeof(Color), typeof(View), default(Color));
+
+ public static Color GetColorTest(BindableObject bindable) => (Color)bindable.GetValue(ColorTestProperty);
+ public static void SetColorTest(BindableObject bindable, Color value) => bindable.SetValue(ColorTestProperty, value);
+ }
+
+ public partial class Bz47950 : ContentPage
+ {
+ public Bz47950()
+ {
+ InitializeComponent();
+ }
+
+ public Bz47950(bool useCompiledXaml)
+ {
+ //this stub will be replaced at compile time
+ }
+
+ [TestFixture]
+ class Tests
+ {
+ [TestCase(true)]
+ [TestCase(false)]
+ public void BehaviorAndStaticResource(bool useCompiledXaml)
+ {
+ var page = new Bz47950(useCompiledXaml);
+ }
+ }
+ }
+}
diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Unreported004.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Unreported004.xaml.cs
index ff2dac08..d4bbeff2 100644
--- a/Xamarin.Forms.Xaml.UnitTests/Issues/Unreported004.xaml.cs
+++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Unreported004.xaml.cs
@@ -43,7 +43,7 @@ namespace Xamarin.Forms.Xaml.UnitTests
[TestCase(true), TestCase(false)]
public void MultipleGetMethodsAllowed(bool useCompiledXaml)
{
- var page = new Unreported004();
+ var page = new Unreported004(useCompiledXaml);
Assert.NotNull(page.label);
Assert.AreEqual("foo", GetSomeProperty(page.label));
}
diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Unreported005.xaml b/Xamarin.Forms.Xaml.UnitTests/Issues/Unreported005.xaml
new file mode 100644
index 00000000..925dbc10
--- /dev/null
+++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Unreported005.xaml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
+ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
+ xmlns:local="clr-namespace:Xamarin.Forms.Xaml.UnitTests"
+ x:Class="Xamarin.Forms.Xaml.UnitTests.Unreported005">
+ <RelativeLayout>
+ <Label x:Name="before" />
+ <Button x:Name="after"
+ RelativeLayout.XConstraint="{local:Unreported005RelativeToViewHorizontal ElementName=before, Constant=105}" />
+
+ </RelativeLayout>
+</ContentPage>
diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Unreported005.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Unreported005.xaml.cs
new file mode 100644
index 00000000..d00f1f41
--- /dev/null
+++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Unreported005.xaml.cs
@@ -0,0 +1,78 @@
+´╗┐using System;
+using NUnit.Framework;
+
+namespace Xamarin.Forms.Xaml.UnitTests
+{
+ public abstract class Unreported005RelativeToView : IMarkupExtension
+ {
+ protected Unreported005RelativeToView()
+ {
+ Factor = 1;
+ }
+
+ public string ElementName { get; set; }
+
+ public double Factor { get; set; }
+
+ public double Constant { get; set; }
+
+ public object ProvideValue(IServiceProvider serviceProvider)
+ {
+ var element = new ReferenceExtension { Name = ElementName }.ProvideValue(serviceProvider) as View;
+ if (element != null) {
+ var result = Constraint.RelativeToView(element, (layout, view) => DeterminePosition(view) + Constant);
+ return result;
+ }
+ return null;
+ }
+
+ protected virtual double DeterminePosition(VisualElement view)
+ {
+ var result = DetermineStart(view) + DetermineExtent(view) * Factor;
+ return result;
+ }
+
+ protected abstract double DetermineExtent(VisualElement view);
+
+ protected abstract double DetermineStart(VisualElement view);
+ }
+
+ public class Unreported005RelativeToViewHorizontal : Unreported005RelativeToView
+ {
+ protected override double DetermineExtent(VisualElement view)
+ {
+ return view.Width;
+ }
+
+ protected override double DetermineStart(VisualElement view)
+ {
+ return view.X;
+ }
+ }
+
+ //[XamlCompilation(XamlCompilationOptions.Skip)]
+ public partial class Unreported005 : ContentPage
+ {
+ public Unreported005()
+ {
+ InitializeComponent();
+ }
+
+ public Unreported005(bool useCompiledXaml)
+ {
+ //this stub will be replaced at compile time
+ }
+
+ [TestFixture]
+ class Tests
+ {
+ [TestCase(true), TestCase(false)]
+ public void CustomMarkupExtensionWorks(bool useCompiledXaml)
+ {
+ var page = new Unreported005(useCompiledXaml);
+ Assert.That(RelativeLayout.GetXConstraint(page.after), Is.TypeOf<Constraint>());
+ Assert.NotNull(RelativeLayout.GetXConstraint(page.after));
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj
index dc7f7ea1..f6f44348 100644
--- a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj
+++ b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj
@@ -371,6 +371,12 @@
<Compile Include="CompiledTypeConverter.xaml.cs" >
<DependentUpon>CompiledTypeConverter.xaml</DependentUpon>
</Compile>
+ <Compile Include="Issues\Unreported005.xaml.cs">
+ <DependentUpon>Unreported005.xaml</DependentUpon>
+ </Compile>
+ <Compile Include="Issues\Bz47950.xaml.cs">
+ <DependentUpon>Bz47950.xaml</DependentUpon>
+ </Compile>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="..\.nuspec\Xamarin.Forms.Debug.targets" />
@@ -659,6 +665,12 @@
<EmbeddedResource Include="CompiledTypeConverter.xaml" >
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
+ <EmbeddedResource Include="Issues\Unreported005.xaml">
+ <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
+ </EmbeddedResource>
+ <EmbeddedResource Include="Issues\Bz47950.xaml">
+ <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
+ </EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />