* [Xaml] AcceptEmptyServiceProvider

* [XamlC] AcceptEmptyServiceProvider

* docs
This commit is contained in:
Stephane Delcroix 2017-02-10 11:07:00 +01:00 коммит произвёл GitHub
Родитель 39f6e6325f
Коммит 3241fae11d
8 изменённых файлов: 136 добавлений и 8 удалений

Просмотреть файл

@ -279,6 +279,7 @@ namespace Xamarin.Forms.Build.Tasks
else if (vardefref.VariableDefinition.VariableType.ImplementsGenericInterface("Xamarin.Forms.Xaml.IMarkupExtension`1",
out markupExtension, out genericArguments))
{
var acceptEmptyServiceProvider = vardefref.VariableDefinition.VariableType.GetCustomAttribute(module.ImportReference(typeof(AcceptEmptyServiceProviderAttribute))) != null;
if (vardefref.VariableDefinition.VariableType.FullName == "Xamarin.Forms.Xaml.BindingExtension")
foreach (var instruction in CompileBindingPath(node, context, vardefref.VariableDefinition))
yield return instruction;
@ -291,6 +292,9 @@ namespace Xamarin.Forms.Build.Tasks
vardefref.VariableDefinition = new VariableDefinition(module.ImportReference(genericArguments.First()));
yield return Instruction.Create(OpCodes.Ldloc, context.Variables[node]);
if (acceptEmptyServiceProvider)
yield return Instruction.Create(OpCodes.Ldnull);
else
foreach (var instruction in node.PushServiceProvider(context, bpRef, propertyRef, propertyDeclaringTypeRef))
yield return instruction;
yield return Instruction.Create(OpCodes.Callvirt, provideValue);
@ -298,12 +302,16 @@ namespace Xamarin.Forms.Build.Tasks
}
else if (context.Variables[node].VariableType.ImplementsInterface(module.ImportReference(typeof (IMarkupExtension))))
{
var acceptEmptyServiceProvider = context.Variables[node].VariableType.GetCustomAttribute(module.ImportReference(typeof(AcceptEmptyServiceProviderAttribute))) != null;
var markExt = module.ImportReference(typeof (IMarkupExtension)).Resolve();
var provideValueInfo = markExt.Methods.First(md => md.Name == "ProvideValue");
var provideValue = module.ImportReference(provideValueInfo);
vardefref.VariableDefinition = new VariableDefinition(module.TypeSystem.Object);
yield return Instruction.Create(OpCodes.Ldloc, context.Variables[node]);
if (acceptEmptyServiceProvider)
yield return Instruction.Create(OpCodes.Ldnull);
else
foreach (var instruction in node.PushServiceProvider(context, bpRef, propertyRef, propertyDeclaringTypeRef))
yield return instruction;
yield return Instruction.Create(OpCodes.Callvirt, provideValue);
@ -311,6 +319,7 @@ namespace Xamarin.Forms.Build.Tasks
}
else if (context.Variables[node].VariableType.ImplementsInterface(module.ImportReference(typeof (IValueProvider))))
{
var acceptEmptyServiceProvider = context.Variables[node].VariableType.GetCustomAttribute(module.ImportReference(typeof(AcceptEmptyServiceProviderAttribute))) != null;
var valueProviderType = context.Variables[node].VariableType;
//If the IValueProvider has a ProvideCompiledAttribute that can be resolved, shortcut this
var compiledValueProviderName = valueProviderType?.GetCustomAttribute(module.ImportReference(typeof(ProvideCompiledAttribute)))?.ConstructorArguments?[0].Value as string;
@ -334,6 +343,9 @@ namespace Xamarin.Forms.Build.Tasks
vardefref.VariableDefinition = new VariableDefinition(module.TypeSystem.Object);
yield return Instruction.Create(OpCodes.Ldloc, context.Variables[node]);
if (acceptEmptyServiceProvider)
yield return Instruction.Create(OpCodes.Ldnull);
else
foreach (var instruction in node.PushServiceProvider(context, bpRef, propertyRef, propertyDeclaringTypeRef))
yield return instruction;
yield return Instruction.Create(OpCodes.Callvirt, provideValue);

Просмотреть файл

@ -11,4 +11,9 @@ namespace Xamarin.Forms.Xaml
{
object ProvideValue(IServiceProvider serviceProvider);
}
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public sealed class AcceptEmptyServiceProviderAttribute : Attribute
{
}
}

Просмотреть файл

@ -0,0 +1,6 @@
<?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.AcceptEmptyServiceProvider" ServiceProvider="{local:Foo}">
</ContentPage>

Просмотреть файл

@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xamarin.Forms;
using Xamarin.Forms.Core.UnitTests;
namespace Xamarin.Forms.Xaml.UnitTests
{
[AcceptEmptyServiceProvider]
public class FooExtension : IMarkupExtension<IServiceProvider>
{
public IServiceProvider ProvideValue(IServiceProvider serviceProvider)
{
return serviceProvider;
}
object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
{
return (this as IMarkupExtension<IServiceProvider>).ProvideValue(serviceProvider);
}
}
public partial class AcceptEmptyServiceProvider : ContentPage
{
public AcceptEmptyServiceProvider()
{
InitializeComponent();
}
public AcceptEmptyServiceProvider(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}
public IServiceProvider ServiceProvider { get; set; }
[TestFixture]
class Tests
{
[SetUp]
public void Setup()
{
Device.PlatformServices = new MockPlatformServices();
}
[TearDown]
public void TearDown()
{
Device.PlatformServices = null;
}
[TestCase(true)]
[TestCase(false)]
public void ServiceProviderIsNullOnAttributedExtensions(bool useCompiledXaml)
{
var p = new AcceptEmptyServiceProvider(useCompiledXaml);
Assert.IsNull(p.ServiceProvider);
}
}
}
}

Просмотреть файл

@ -441,6 +441,10 @@
</Compile>
<Compile Include="Issues\Bz44216.xaml.cs">
<DependentUpon>Bz44216.xaml</DependentUpon>
<DependentUpon>AcceptEmptyServiceProvider.xaml</DependentUpon>
</Compile>
<Compile Include="AcceptEmptyServiceProvider.xaml.cs">
<DependentUpon>AcceptEmptyServiceProvider.xaml</DependentUpon>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
@ -805,6 +809,9 @@
<EmbeddedResource Include="Issues\Bz44216.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="AcceptEmptyServiceProvider.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />

Просмотреть файл

@ -91,12 +91,12 @@ namespace Xamarin.Forms.Xaml
var valueProvider = value as IValueProvider;
if (markupExtension != null) {
var serviceProvider = new XamlServiceProvider(node, Context);
var serviceProvider = value.GetType().GetTypeInfo().GetCustomAttribute<AcceptEmptyServiceProviderAttribute>() == null ? new XamlServiceProvider(node, Context) : null;
value = markupExtension.ProvideValue(serviceProvider);
}
if (valueProvider != null) {
var serviceProvider = new XamlServiceProvider(node, Context);
var serviceProvider = value.GetType().GetTypeInfo().GetCustomAttribute<AcceptEmptyServiceProviderAttribute>() == null ? new XamlServiceProvider(node, Context) : null;
value = valueProvider.ProvideValue(serviceProvider);
}

Просмотреть файл

@ -0,0 +1,36 @@
<Type Name="AcceptEmptyServiceProviderAttribute" FullName="Xamarin.Forms.Xaml.AcceptEmptyServiceProviderAttribute">
<TypeSignature Language="C#" Value="public sealed class AcceptEmptyServiceProviderAttribute : Attribute" />
<TypeSignature Language="ILAsm" Value=".class public auto ansi sealed beforefieldinit AcceptEmptyServiceProviderAttribute extends System.Attribute" />
<AssemblyInfo>
<AssemblyName>Xamarin.Forms.Core</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Base>
<BaseTypeName>System.Attribute</BaseTypeName>
</Base>
<Interfaces />
<Attributes>
<Attribute>
<AttributeName>System.AttributeUsage(System.AttributeTargets.Class, Inherited=false)</AttributeName>
</Attribute>
</Attributes>
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
<Members>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public AcceptEmptyServiceProviderAttribute ();" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor() cil managed" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters />
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
</Member>
</Members>
</Type>

Просмотреть файл

@ -494,6 +494,7 @@
<Type Name="ToolbarPlacement" Kind="Enumeration" />
</Namespace>
<Namespace Name="Xamarin.Forms.Xaml">
<Type Name="AcceptEmptyServiceProviderAttribute" Kind="Class" />
<Type Name="IMarkupExtension" Kind="Interface" />
<Type Name="IMarkupExtension`1" DisplayName="IMarkupExtension&lt;T&gt;" Kind="Interface" />
<Type Name="IProvideValueTarget" Kind="Interface" />