diff --git a/src/Controls/src/Xaml/MarkupExtensions/BindingExtension.cs b/src/Controls/src/Xaml/MarkupExtensions/BindingExtension.cs index 552b8c6627b6..bbbe745a889e 100644 --- a/src/Controls/src/Xaml/MarkupExtensions/BindingExtension.cs +++ b/src/Controls/src/Xaml/MarkupExtensions/BindingExtension.cs @@ -43,7 +43,8 @@ BindingBase IMarkupExtension.ProvideValue(IServiceProvider serviceP BindingBase CreateBinding() { Type bindingXDataType = null; - if ((serviceProvider.GetService(typeof(IXamlTypeResolver)) is IXamlTypeResolver typeResolver) + if (serviceProvider is not null && + (serviceProvider.GetService(typeof(IXamlTypeResolver)) is IXamlTypeResolver typeResolver) && (serviceProvider.GetService(typeof(IXamlDataTypeProvider)) is IXamlDataTypeProvider dataTypeProvider) && dataTypeProvider.BindingDataType != null) { diff --git a/src/Controls/tests/Xaml.UnitTests/BindingExtensionTests.cs b/src/Controls/tests/Xaml.UnitTests/BindingExtensionTests.cs new file mode 100644 index 000000000000..2bd83b0ab410 --- /dev/null +++ b/src/Controls/tests/Xaml.UnitTests/BindingExtensionTests.cs @@ -0,0 +1,14 @@ +using NUnit.Framework; + +namespace Microsoft.Maui.Controls.Xaml.UnitTests; + +[TestFixture] +public class BindingExtensionTests +{ + [Test] + public void ProvideValue_Null() + { + IMarkupExtension binding = new BindingExtension { Path = "Foo" }; + binding.ProvideValue(null); // This should not throw + } +}