diff --git a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs
index 64bc75859..8b2a25fb3 100644
--- a/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs
+++ b/Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs
@@ -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,26 +292,34 @@ namespace Xamarin.Forms.Build.Tasks
vardefref.VariableDefinition = new VariableDefinition(module.ImportReference(genericArguments.First()));
yield return Instruction.Create(OpCodes.Ldloc, context.Variables[node]);
- foreach (var instruction in node.PushServiceProvider(context, bpRef, propertyRef, propertyDeclaringTypeRef))
- yield return instruction;
+ 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);
yield return Instruction.Create(OpCodes.Stloc, vardefref.VariableDefinition);
}
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]);
- foreach (var instruction in node.PushServiceProvider(context, bpRef, propertyRef, propertyDeclaringTypeRef))
- yield return instruction;
+ 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);
yield return Instruction.Create(OpCodes.Stloc, vardefref.VariableDefinition);
}
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,8 +343,11 @@ namespace Xamarin.Forms.Build.Tasks
vardefref.VariableDefinition = new VariableDefinition(module.TypeSystem.Object);
yield return Instruction.Create(OpCodes.Ldloc, context.Variables[node]);
- foreach (var instruction in node.PushServiceProvider(context, bpRef, propertyRef, propertyDeclaringTypeRef))
- yield return instruction;
+ 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);
yield return Instruction.Create(OpCodes.Stloc, vardefref.VariableDefinition);
}
diff --git a/Xamarin.Forms.Core/IMarkupExtension.cs b/Xamarin.Forms.Core/IMarkupExtension.cs
index 24a435f9d..2b32f3894 100644
--- a/Xamarin.Forms.Core/IMarkupExtension.cs
+++ b/Xamarin.Forms.Core/IMarkupExtension.cs
@@ -11,4 +11,9 @@ namespace Xamarin.Forms.Xaml
{
object ProvideValue(IServiceProvider serviceProvider);
}
+
+ [AttributeUsage(AttributeTargets.Class, Inherited = false)]
+ public sealed class AcceptEmptyServiceProviderAttribute : Attribute
+ {
+ }
}
\ No newline at end of file
diff --git a/Xamarin.Forms.Xaml.UnitTests/AcceptEmptyServiceProvider.xaml b/Xamarin.Forms.Xaml.UnitTests/AcceptEmptyServiceProvider.xaml
new file mode 100644
index 000000000..b39bd2878
--- /dev/null
+++ b/Xamarin.Forms.Xaml.UnitTests/AcceptEmptyServiceProvider.xaml
@@ -0,0 +1,6 @@
+
+
+
\ No newline at end of file
diff --git a/Xamarin.Forms.Xaml.UnitTests/AcceptEmptyServiceProvider.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/AcceptEmptyServiceProvider.xaml.cs
new file mode 100644
index 000000000..611cb36e8
--- /dev/null
+++ b/Xamarin.Forms.Xaml.UnitTests/AcceptEmptyServiceProvider.xaml.cs
@@ -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
+ {
+ public IServiceProvider ProvideValue(IServiceProvider serviceProvider)
+ {
+ return serviceProvider;
+ }
+
+ object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
+ {
+ return (this as IMarkupExtension).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);
+ }
+ }
+ }
+}
diff --git a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj
index 4b3e5c86c..90bab284d 100644
--- a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj
+++ b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj
@@ -441,6 +441,10 @@
Bz44216.xaml
+ AcceptEmptyServiceProvider.xaml
+
+
+ AcceptEmptyServiceProvider.xaml
@@ -805,6 +809,9 @@
MSBuild:UpdateDesignTimeXaml
+
+ MSBuild:UpdateDesignTimeXaml
+
diff --git a/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs b/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs
index 833d5d949..5b8a84030 100644
--- a/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs
+++ b/Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs
@@ -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() == 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() == null ? new XamlServiceProvider(node, Context) : null;
value = valueProvider.ProvideValue(serviceProvider);
}
diff --git a/docs/Xamarin.Forms.Core/Xamarin.Forms.Xaml/AcceptEmptyServiceProviderAttribute.xml b/docs/Xamarin.Forms.Core/Xamarin.Forms.Xaml/AcceptEmptyServiceProviderAttribute.xml
new file mode 100644
index 000000000..d95dffa3e
--- /dev/null
+++ b/docs/Xamarin.Forms.Core/Xamarin.Forms.Xaml/AcceptEmptyServiceProviderAttribute.xml
@@ -0,0 +1,36 @@
+
+
+
+
+ Xamarin.Forms.Core
+ 2.0.0.0
+
+
+ System.Attribute
+
+
+
+
+ System.AttributeUsage(System.AttributeTargets.Class, Inherited=false)
+
+
+
+ To be added.
+ To be added.
+
+
+
+
+
+ Constructor
+
+ 2.0.0.0
+
+
+
+ To be added.
+ To be added.
+
+
+
+
diff --git a/docs/Xamarin.Forms.Core/index.xml b/docs/Xamarin.Forms.Core/index.xml
index 89af75c21..1b1cf3100 100644
--- a/docs/Xamarin.Forms.Core/index.xml
+++ b/docs/Xamarin.Forms.Core/index.xml
@@ -494,6 +494,7 @@
+