Skip to content

NullReferenceException with a platform-specific GestureRecognizer #24900

@Jean-RobertLaffineur

Description

@Jean-RobertLaffineur

Description

On Windows (didn't check the other platforms), a NullReferenceException occurs when adding a GestureRecognizer that is conditioned by an OnPlatform, such as no GestureRecognizer is defined in the current (Windows) platform:

<Border>
    <Border.GestureRecognizers>
        <OnPlatform x:TypeArguments="GestureRecognizer">
            <OnPlatform.Platforms>
                <On Platform="Android">
                    <TapGestureRecognizer Tapped="Header_Tapped" />
                </On>
                <!--<On Platform="WinUI">
                    <TapGestureRecognizer Tapped="Header_Tapped" />
                </On>-->
            </OnPlatform.Platforms>
        </OnPlatform>
    </Border.GestureRecognizers>
</Border>

Un-commenting the WinUI part above makes the NullReferenceException disappear.

Attached the complete reproduction project, which is basically a empty MAUI App project with the XAML definition from above.

GestureRecognizerCrash.zip

Steps to Reproduce

  1. Open the attached project
  2. Build and run the project in Windows

Result: crash with a NullReferenceException

Link to public reproduction project repository

No response

Version with bug

8.0.70 SR7

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.61 SR6.1

Affected platforms

Windows

Affected platform versions

Windows 10.0.19041.0

Did you find any workaround?

Un-commenting the lines in the XAML extract above fixes the crash.

Downgrading both dependencies (maybe one is enough?) below from version 8.0.70 to version 8.0.61 also fix the crash:

  • Microsoft.Maui.Controls
  • Microsoft.Maui.Controls.Compatibility

Relevant log output

Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.View..ctor.__AddItems|15_1(System.Collections.IList newItems)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.View..ctor.AnonymousMethod__15_0(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args)	Unknown
>	System.ObjectModel.dll!System.Collections.ObjectModel.ObservableCollection<Microsoft.Maui.Controls.IGestureRecognizer>.OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) Line 192	C#
 	[Native to Managed Transition]	
 	[Managed to Native Transition]	
 	System.Private.CoreLib.dll!System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(object obj, System.Span<object> copyOfArgs, System.Reflection.BindingFlags invokeAttr) Line 178	C#
 	System.Private.CoreLib.dll!System.Reflection.MethodBaseInvoker.InvokeWithOneArg(object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] parameters, System.Globalization.CultureInfo culture) Line 104	C#
 	Microsoft.Maui.Controls.Xaml.dll!Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.TryAddToProperty(object element, Microsoft.Maui.Controls.Xaml.XmlName propertyName, object value, string xKey, System.Xml.IXmlLineInfo lineInfo, System.IServiceProvider serviceProvider, object rootElement, out System.Exception exception)	Unknown
 	Microsoft.Maui.Controls.Xaml.dll!Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.TrySetPropertyValue(object element, Microsoft.Maui.Controls.Xaml.XmlName propertyName, string xKey, object value, object rootElement, System.Xml.IXmlLineInfo lineInfo, System.IServiceProvider serviceProvider, out System.Exception xpe)	Unknown
 	Microsoft.Maui.Controls.Xaml.dll!Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.SetPropertyValue(object xamlelement, Microsoft.Maui.Controls.Xaml.XmlName propertyName, object value, object rootElement, Microsoft.Maui.Controls.Xaml.INode node, Microsoft.Maui.Controls.Xaml.HydrationContext context, System.Xml.IXmlLineInfo lineInfo)	Unknown
 	Microsoft.Maui.Controls.Xaml.dll!Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.Visit(Microsoft.Maui.Controls.Xaml.ElementNode node, Microsoft.Maui.Controls.Xaml.INode parentNode)	Unknown
 	Microsoft.Maui.Controls.Xaml.dll!Microsoft.Maui.Controls.Xaml.ElementNode.Accept(Microsoft.Maui.Controls.Xaml.IXamlNodeVisitor visitor, Microsoft.Maui.Controls.Xaml.INode parentNode)	Unknown
 	Microsoft.Maui.Controls.Xaml.dll!Microsoft.Maui.Controls.Xaml.ElementNode.Accept(Microsoft.Maui.Controls.Xaml.IXamlNodeVisitor visitor, Microsoft.Maui.Controls.Xaml.INode parentNode)	Unknown
 	Microsoft.Maui.Controls.Xaml.dll!Microsoft.Maui.Controls.Xaml.RootNode.Accept(Microsoft.Maui.Controls.Xaml.IXamlNodeVisitor visitor, Microsoft.Maui.Controls.Xaml.INode parentNode)	Unknown
 	Microsoft.Maui.Controls.Xaml.dll!Microsoft.Maui.Controls.Xaml.XamlLoader.Visit(Microsoft.Maui.Controls.Xaml.RootNode rootnode, Microsoft.Maui.Controls.Xaml.HydrationContext visitorContext, bool useDesignProperties)	Unknown
 	Microsoft.Maui.Controls.Xaml.dll!Microsoft.Maui.Controls.Xaml.XamlLoader.Load(object view, string xaml, System.Reflection.Assembly rootAssembly, bool useDesignProperties)	Unknown
 	Microsoft.Maui.Controls.Xaml.dll!Microsoft.Maui.Controls.Xaml.XamlLoader.Load(object view, string xaml, bool useDesignProperties)	Unknown
 	Microsoft.Maui.Controls.Xaml.dll!Microsoft.Maui.Controls.Xaml.XamlLoader.Load(object view, System.Type callingType)	Unknown
 	Microsoft.Maui.Controls.Xaml.dll!Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml<GestureRecognizerCrash.MainPage>(GestureRecognizerCrash.MainPage view, System.Type callingType)	Unknown
 	GestureRecognizerCrash.dll!GestureRecognizerCrash.MainPage.InitializeComponent() Line 22	C#
 	GestureRecognizerCrash.dll!GestureRecognizerCrash.MainPage.MainPage() Line 9	C#
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.ShellContent.Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent.AnonymousMethod__0()	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.ElementTemplate.CreateContent()	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Internals.DataTemplateExtensions.CreateContent(Microsoft.Maui.Controls.DataTemplate self, object item, Microsoft.Maui.Controls.BindableObject container)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.ShellContent.Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent()	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Handlers.ShellContentHandler.CreatePlatformElement()	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Handlers.ElementHandler<System.__Canon, System.__Canon>.OnCreatePlatformElement()	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Handlers.ElementHandler.CreatePlatformElement()	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(Microsoft.Maui.IElement view)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Element.SetHandler(Microsoft.Maui.IElementHandler newHandler)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Element.Handler.set(Microsoft.Maui.IElementHandler value)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Platform.ElementExtensions.ToHandler(Microsoft.Maui.IElement view, Microsoft.Maui.IMauiContext context)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Platform.ElementExtensions.ToPlatform(Microsoft.Maui.IElement view, Microsoft.Maui.IMauiContext context)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Handlers.ShellSectionHandler.SyncNavigationStack(bool animated, Microsoft.Maui.Controls.Internals.NavigationRequestedEventArgs e)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Handlers.ShellSectionHandler.MapCurrentItem(Microsoft.Maui.Controls.Handlers.ShellSectionHandler handler, Microsoft.Maui.Controls.ShellSection item)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.PropertyMapper<Microsoft.Maui.Controls.ShellSection, Microsoft.Maui.Controls.Handlers.ShellSectionHandler>.Add.AnonymousMethod__0(Microsoft.Maui.IElementHandler h, Microsoft.Maui.IElement v)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.PropertyMapper.UpdatePropertyCore(string key, Microsoft.Maui.IElementHandler viewHandler, Microsoft.Maui.IElement virtualView)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.PropertyMapper.UpdateProperties(Microsoft.Maui.IElementHandler viewHandler, Microsoft.Maui.IElement virtualView)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(Microsoft.Maui.IElement view)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Handlers.ShellSectionHandler.SetVirtualView(Microsoft.Maui.IElement view)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Element.SetHandler(Microsoft.Maui.IElementHandler newHandler)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Element.Handler.set(Microsoft.Maui.IElementHandler value)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Platform.ElementExtensions.ToHandler(Microsoft.Maui.IElement view, Microsoft.Maui.IMauiContext context)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Handlers.ShellItemHandler.UpdateCurrentItem()	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Handlers.ShellItemHandler.MapCurrentItem(Microsoft.Maui.Controls.Handlers.ShellItemHandler handler, Microsoft.Maui.Controls.ShellItem item)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.PropertyMapper<Microsoft.Maui.Controls.ShellItem, Microsoft.Maui.Controls.Handlers.ShellItemHandler>.Add.AnonymousMethod__0(Microsoft.Maui.IElementHandler h, Microsoft.Maui.IElement v)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.PropertyMapper.UpdatePropertyCore(string key, Microsoft.Maui.IElementHandler viewHandler, Microsoft.Maui.IElement virtualView)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.PropertyMapper.UpdateProperties(Microsoft.Maui.IElementHandler viewHandler, Microsoft.Maui.IElement virtualView)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(Microsoft.Maui.IElement view)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Handlers.ShellItemHandler.SetVirtualView(Microsoft.Maui.IElement view)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Element.SetHandler(Microsoft.Maui.IElementHandler newHandler)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Element.Handler.set(Microsoft.Maui.IElementHandler value)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Platform.ElementExtensions.ToHandler(Microsoft.Maui.IElement view, Microsoft.Maui.IMauiContext context)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Platform.ShellView.CreateShellItemView()	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Platform.ShellView.SwitchShellItem(Microsoft.Maui.Controls.ShellItem newItem, bool animate)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Handlers.ShellHandler.MapCurrentItem(Microsoft.Maui.Controls.Handlers.ShellHandler handler, Microsoft.Maui.Controls.Shell view)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.PropertyMapper<Microsoft.Maui.Controls.Shell, Microsoft.Maui.Controls.Handlers.ShellHandler>.Add.AnonymousMethod__0(Microsoft.Maui.IElementHandler h, Microsoft.Maui.IElement v)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.PropertyMapper.UpdatePropertyCore(string key, Microsoft.Maui.IElementHandler viewHandler, Microsoft.Maui.IElement virtualView)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.PropertyMapper.UpdateProperties(Microsoft.Maui.IElementHandler viewHandler, Microsoft.Maui.IElement virtualView)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(Microsoft.Maui.IElement view)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Handlers.ViewHandler<System.__Canon, System.__Canon>.SetVirtualView(Microsoft.Maui.IView view)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Handlers.ShellHandler.SetVirtualView(Microsoft.Maui.IView view)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Handlers.ViewHandler<System.__Canon, System.__Canon>.SetVirtualView(Microsoft.Maui.IElement view)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Element.SetHandler(Microsoft.Maui.IElementHandler newHandler)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Element.Handler.set(Microsoft.Maui.IElementHandler value)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.Handler.set(Microsoft.Maui.IElementHandler value)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Platform.ElementExtensions.ToHandler(Microsoft.Maui.IElement view, Microsoft.Maui.IMauiContext context)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Platform.ElementExtensions.ToPlatform(Microsoft.Maui.IElement view, Microsoft.Maui.IMauiContext context)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Handlers.WindowHandler.MapContent(Microsoft.Maui.Handlers.IWindowHandler handler, Microsoft.Maui.IWindow window)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.PropertyMapper<Microsoft.Maui.IWindow, Microsoft.Maui.Handlers.IWindowHandler>.Add.AnonymousMethod__0(Microsoft.Maui.IElementHandler h, Microsoft.Maui.IElement v)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.PropertyMapper.UpdatePropertyCore(string key, Microsoft.Maui.IElementHandler viewHandler, Microsoft.Maui.IElement virtualView)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.PropertyMapper.UpdateProperties(Microsoft.Maui.IElementHandler viewHandler, Microsoft.Maui.IElement virtualView)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(Microsoft.Maui.IElement view)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Element.SetHandler(Microsoft.Maui.IElementHandler newHandler)	Unknown
 	Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Element.Handler.set(Microsoft.Maui.IElementHandler value)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Platform.ElementExtensions.SetHandler(WinRT.IWinRTObject nativeElement, Microsoft.Maui.IElement element, Microsoft.Maui.IMauiContext context)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Platform.ElementExtensions.SetWindowHandler(Microsoft.UI.Xaml.Window platformWindow, Microsoft.Maui.IWindow window, Microsoft.Maui.IMauiContext context)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Platform.ApplicationExtensions.CreatePlatformWindow(Microsoft.UI.Xaml.Application platformApplication, Microsoft.Maui.IApplication application, Microsoft.Maui.Handlers.OpenWindowRequest args)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.Platform.ApplicationExtensions.CreatePlatformWindow(Microsoft.UI.Xaml.Application platformApplication, Microsoft.Maui.IApplication application, Microsoft.UI.Xaml.LaunchActivatedEventArgs args)	Unknown
 	Microsoft.Maui.dll!Microsoft.Maui.MauiWinUIApplication.OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)	Unknown
 	Microsoft.WinUI.dll!Microsoft.UI.Xaml.Application.Microsoft.UI.Xaml.IApplicationOverrides.OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args) Line 387	C#
 	Microsoft.WinUI.dll!ABI.Microsoft.UI.Xaml.IApplicationOverrides.Do_Abi_OnLaunched_0(nint thisPtr, nint args) Line 13722	C#
 	[Native to Managed Transition]	
 	[Managed to Native Transition]	
 	Microsoft.WinUI.dll!ABI.Microsoft.UI.Xaml.IApplicationStaticsMethods.Start(WinRT.IObjectReference _obj, Microsoft.UI.Xaml.ApplicationInitializationCallback callback) Line 13745	C#
 	Microsoft.WinUI.dll!Microsoft.UI.Xaml.Application.Start(Microsoft.UI.Xaml.ApplicationInitializationCallback callback) Line 320	C#
 	GestureRecognizerCrash.dll!GestureRecognizerCrash.WinUI.Program.Main(string[] args) Line 32	C#

Metadata

Metadata

Labels

area-gesturesGesture typesfixed-in-8.0.92i/regressionThis issue described a confirmed regression on a currently supported versionplatform/windowss/triagedIssue has been revieweds/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't working

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions