diff options
5 files changed, 76 insertions, 2 deletions
diff --git a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs index 4a8fd70c..8d087ab7 100644 --- a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs +++ b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs @@ -724,7 +724,7 @@ namespace Xamarin.Forms.Build.Tasks if (CanAdd(parent, localName, valueNode, context)) return Add(parent, localName, valueNode, iXmlLineInfo, context); - throw new XamlParseException($"No property, bindable property, or event found for '{localName}'", iXmlLineInfo); + throw new XamlParseException($"No property, bindable property, or event found for '{localName}', or mismatching type between value and property.", iXmlLineInfo); } static FieldReference GetBindablePropertyReference(VariableDefinition parent, string namespaceURI, ref string localName, out bool attached, ILContext context, IXmlLineInfo iXmlLineInfo) @@ -838,6 +838,10 @@ namespace Xamarin.Forms.Build.Tasks VariableDefinition varValue; if (!context.Variables.TryGetValue(valueNode as IElementNode, out varValue)) return false; + var implicitOperator = varValue.VariableType.GetImplicitOperatorTo(module.Import(typeof(BindingBase)), module); + if (implicitOperator != null) + return true; + return varValue.VariableType.InheritsFromOrImplements(module.Import(typeof(BindingBase))); } @@ -845,6 +849,7 @@ namespace Xamarin.Forms.Build.Tasks { var module = context.Body.Method.Module; var varValue = context.Variables [elementNode]; + var implicitOperator = varValue.VariableType.GetImplicitOperatorTo(module.Import(typeof(BindingBase)), module); //TODO: check if parent is a BP var setBinding = typeof(BindableObject).GetMethod("SetBinding", new [] { typeof(BindableProperty), typeof(BindingBase) }); @@ -852,6 +857,9 @@ namespace Xamarin.Forms.Build.Tasks yield return Instruction.Create(OpCodes.Ldloc, parent); yield return Instruction.Create(OpCodes.Ldsfld, bpRef); yield return Instruction.Create(OpCodes.Ldloc, varValue); + if (implicitOperator != null) +// IL_000f: call !0 class [Xamarin.Forms.Core]Xamarin.Forms.OnPlatform`1<BindingBase>::op_Implicit(class [Xamarin.Forms.Core]Xamarin.Forms.OnPlatform`1<!0>) + yield return Instruction.Create(OpCodes.Call, module.Import(implicitOperator)); yield return Instruction.Create(OpCodes.Callvirt, module.Import(setBinding)); } diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz44213.xaml b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz44213.xaml new file mode 100644 index 00000000..0a932f32 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz44213.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" x:Class="Xamarin.Forms.Xaml.UnitTests.Bz44213"> + <Label x:Name="label"> + <Label.Text> + <OnPlatform x:TypeArguments="BindingBase" Android="{Binding Bar}"> + <OnPlatform.iOS> + <Binding Path="Foo"/> + </OnPlatform.iOS> + </OnPlatform> + </Label.Text> + </Label> +</ContentPage> diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz44213.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz44213.xaml.cs new file mode 100644 index 00000000..faabad6d --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz44213.xaml.cs @@ -0,0 +1,48 @@ +using NUnit.Framework; +using Xamarin.Forms.Core.UnitTests; + +namespace Xamarin.Forms.Xaml.UnitTests +{ + public partial class Bz44213 : ContentPage + { + public Bz44213() + { + InitializeComponent(); + } + + public Bz44213(bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + + [TestFixture] + class Tests + { + [SetUp] + public void Setup() + { + Device.PlatformServices = new MockPlatformServices(); + } + + [TearDown] + public void TearDown() + { + Device.PlatformServices = null; + } + + [TestCase(true)] + [TestCase(false)] + public void BindingInOnPlatform(bool useCompiledXaml) + { + ((MockPlatformServices)Device.PlatformServices).RuntimePlatform = Device.iOS; + var p = new Bz44213(useCompiledXaml); + p.BindingContext = new { Foo = "Foo", Bar = "Bar" }; + Assert.AreEqual("Foo", p.label.Text); + ((MockPlatformServices)Device.PlatformServices).RuntimePlatform = Device.Android; + p = new Bz44213(useCompiledXaml); + p.BindingContext = new { Foo = "Foo", Bar = "Bar" }; + Assert.AreEqual("Bar", p.label.Text); + } + } + } +}
\ 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 631d0010..5649861b 100644 --- a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj +++ b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj @@ -424,6 +424,9 @@ <Compile Include="Issues\Bz43733.xaml.cs"> <DependentUpon>Bz43733.xaml</DependentUpon> </Compile> + <Compile Include="Issues\Bz44213.xaml.cs"> + <DependentUpon>Bz44213.xaml</DependentUpon> + </Compile> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="..\.nuspec\Xamarin.Forms.Debug.targets" /> @@ -769,6 +772,9 @@ <EmbeddedResource Include="Issues\Bz43733.xaml"> <Generator>MSBuild:UpdateDesignTimeXaml</Generator> </EmbeddedResource> + <EmbeddedResource Include="Issues\Bz44213.xaml"> + <Generator>MSBuild:UpdateDesignTimeXaml</Generator> + </EmbeddedResource> </ItemGroup> <ItemGroup> <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" /> diff --git a/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs b/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs index 879ea3b0..47ef9f6c 100644 --- a/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs +++ b/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs @@ -361,7 +361,7 @@ namespace Xamarin.Forms.Xaml exception = null; var elementType = element.GetType(); - var binding = value as BindingBase; + var binding = value.ConvertTo(typeof(BindingBase),pinfoRetriever:null,serviceProvider:null) as BindingBase; var bindable = element as BindableObject; var nativeBindingService = DependencyService.Get<INativeBindingService>(); |