Skip to content

System.InvalidCastException when using QueryPropertyAttribute with nullable types #33420

@meyerdominik

Description

@meyerdominik

Description

I’m unable to pass nullable values via QueryPropertyAttribute during Shell navigation in a .NET MAUI app. When navigating from one view to another and including a nullable parameter, the application crashes with a System.InvalidCastException.

The exception appears to originate from MAUI’s internal query parameter handling, specifically during ShellContent.ApplyQueryAttributes, where the framework attempts to convert the value and fails if the target property is nullable.

Stack trace:

[Exception] System.Private.CoreLib.dll!System.Convert.DefaultToType(System.IConvertible value, System.Type targetType, System.IFormatProvider provider) Unknown [Exception] Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.ShellContent.ApplyQueryAttributes(object content, Microsoft.Maui.Controls.ShellRouteParameters query, Microsoft.Maui.Controls.ShellRouteParameters oldQuery) Unknown [Exception] Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.ShellContent.OnQueryAttributesPropertyChanged(Microsoft.Maui.Controls.BindableObject bindable, object oldValue, object newValue) Unknown [Exception] Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.BindableObject.OnBindablePropertySet(Microsoft.Maui.Controls.BindableProperty property, object original, object value, bool didChange, bool willFirePropertyChanged) Unknown [Exception] Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Element.OnBindablePropertySet(Microsoft.Maui.Controls.BindableProperty property, object original, object value, bool changed, bool willFirePropertyChanged) Unknown [Exception] Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.BindableObject.SetValueActual(Microsoft.Maui.Controls.BindableProperty property, Microsoft.Maui.Controls.BindableObject.BindablePropertyContext context, object value, bool currentlyApplying, Microsoft.Maui.Controls.Internals.SetValueFlags attributes, Microsoft.Maui.Controls.SetterSpecificity specificity, bool silent) Unknown [Exception] Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.BindableObject.SetValueCore(Microsoft.Maui.Controls.BindableProperty property, object value, Microsoft.Maui.Controls.Internals.SetValueFlags attributes, Microsoft.Maui.Controls.BindableObject.SetValuePrivateFlags privateAttributes, Microsoft.Maui.Controls.SetterSpecificity specificity) Unknown [Exception] Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.BindableObject.SetValue(Microsoft.Maui.Controls.BindableProperty property, object value) Unknown [Exception] Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.ShellNavigationManager.ApplyQueryAttributes(Microsoft.Maui.Controls.Element element, Microsoft.Maui.Controls.ShellRouteParameters query, bool isLastItem, bool isPopping) Unknown [Exception] Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.ShellSection.GetOrCreateFromRoute(string route, Microsoft.Maui.Controls.ShellRouteParameters queryData, System.IServiceProvider services, bool isLast, bool isPopping) Unknown [Exception] Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.ShellSection.GoToAsync(Microsoft.Maui.Controls.ShellNavigationRequest request, Microsoft.Maui.Controls.ShellRouteParameters queryData, System.IServiceProvider services, bool? animate, bool isRelativePopping) Unknown [Exception] Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.ShellNavigationManager.GoToAsync(Microsoft.Maui.Controls.ShellNavigationParameters shellNavigationParameters, Microsoft.Maui.Controls.ShellNavigationRequest navigationRequest) Unknown System.Private.CoreLib.dll!System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() Unknown System.Private.CoreLib.dll!System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task task) Unknown System.Private.CoreLib.dll!System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task task, System.Threading.Tasks.ConfigureAwaitOptions options) Unknown [Waiting on Async Operation, double-click or press enter to view Async Call Stacks] > MauiTest.dll!MauiTest.MainPage.OnCounterClicked(object sender, System.EventArgs e) Line 17 C# Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Button.Microsoft.Maui.Controls.Internals.IButtonElement.PropagateUpClicked() Unknown Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.ButtonElement.ElementClicked(Microsoft.Maui.Controls.VisualElement visualElement, Microsoft.Maui.Controls.Internals.IButtonElement ButtonElementManager) Unknown Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Button.SendClicked() Unknown Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Button.Microsoft.Maui.IButton.Clicked() Unknown Microsoft.Maui.dll!Microsoft.Maui.Handlers.ButtonHandler.OnClick(object sender, Microsoft.UI.Xaml.RoutedEventArgs e) Unknown Microsoft.WinUI.dll!WinRT._EventSource_global__Microsoft_UI_Xaml_RoutedEventHandler.EventState.GetEventInvoke.AnonymousMethod__1_0(object sender, Microsoft.UI.Xaml.RoutedEventArgs e) Unknown Microsoft.WinUI.dll!ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(nint thisPtr, nint sender, nint e) Unknown [Native to Managed Transition] [Managed to Native Transition] Microsoft.WinUI.dll!ABI.Microsoft.UI.Xaml.Controls.IControlOverridesMethods.OnPointerReleased(WinRT.IObjectReference _obj, Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e) Unknown Microsoft.WinUI.dll!Microsoft.UI.Xaml.Controls.Control.OnPointerReleased(Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e) Unknown Microsoft.WinUI.dll!Microsoft.UI.Xaml.Controls.Control.Microsoft.UI.Xaml.Controls.IControlOverrides.OnPointerReleased(Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e) Unknown Microsoft.WinUI.dll!ABI.Microsoft.UI.Xaml.Controls.IControlOverrides.Do_Abi_OnPointerReleased_3(nint thisPtr, nint e) Unknown [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) Unknown Microsoft.WinUI.dll!Microsoft.UI.Xaml.Application.Start(Microsoft.UI.Xaml.ApplicationInitializationCallback callback) Unknown MauiTest.dll!MauiTest.WinUI.Program.Main(string[] args) Line 26 C#

Workload version: 10.0.100-manifests.9f71effe
Installed Workload Id Manifest Version Installation Source
android 36.1.2/10.0.100 VS 18.1.11312.151
ios 26.1.10502/10.0.100 VS 18.1.11312.151
maccatalyst 26.1.10502/10.0.100 VS 18.1.11312.151
maui-windows 10.0.1/10.0.100 VS 18.1.11312.151

Steps to Reproduce

  1. Clone and run the public reproduction project
  2. Launch the app.
  3. Click the "Click me" button on the main page.
  4. Observe the app crashing with a System.InvalidCastException.

Link to public reproduction project repository

https://github.com/meyerdominik/MauiTest

Version with bug

10.0.20

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

Windows, Android, I was not able test on other platforms

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions