зеркало из https://github.com/DeGsoft/maui-linux.git
[XamlC] support non-generic IMarkup on ABPs (#562)
* [XamlC] support custom markups on ABPs * [XamlC] test for 47950
This commit is contained in:
Родитель
26d5b2b803
Коммит
70d29b9e92
|
@ -94,9 +94,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));
|
||||
|
@ -675,7 +676,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))
|
||||
|
@ -690,7 +692,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
|
||||
|
@ -704,14 +706,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 &&
|
||||
|
@ -832,7 +834,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;
|
||||
|
||||
|
@ -851,6 +853,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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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>
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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>
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -373,6 +373,12 @@
|
|||
<Compile Include="BindingsCompiler.xaml.cs">
|
||||
<DependentUpon>BindingsCompiler.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" />
|
||||
|
@ -667,6 +673,12 @@
|
|||
<EmbeddedResource Include="BindingsCompiler.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}" />
|
||||
|
|
Загрузка…
Ссылка в новой задаче