Skip to content

Crash on Windows when UseMauiCompatibility is enabled ("The handler's MauiContext cannot be null.") #11600

@holecekp

Description

@holecekp

Description

Edited: When I created the issue I made a mistake and switched the situations when UseMauiCompatibility() is used, and when it is not used. I have corrected it in the issue title and the description.

My app started to crash on various pages after upgrade to .NET 7. I had spent a few hours comparing the crashing and non-crashing projects to find the issue because this bug is a tricky one. It turned out that the difference that causes the crashes is the presence/absence of UseMauiCompatibility() in MauiProgram even though no legacy features or controls are used.

I think that it worked in the previous .NET 6 version but I am not sure.

Please try the following repro project https://github.com/holecekp/MauiBugCrashInLazyView. After clicking the button, a control is lazy-loaded. This works on Android but it crashes on Windows with the error message:
"The handler's MauiContext cannot be null."

MauiBugLazyViews

The crash occurs when a new view is created at runtime and is assigned into ContentView.Content.

Steps to Reproduce

  1. Clone repro project https://github.com/holecekp/MauiBugCrashInLazyView
  2. Run it on Windows and click the button.
  3. The app will crash.
  4. Repeat the same for Android. The app will not crash.
  5. Comment out UseMauiCompatibility() in MauiProgram and repeat the steps again for Windows. This time, the app will not crash.

Link to public reproduction project repository

https://github.com/holecekp/MauiBugCrashInLazyView

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

Windows 10

Did you find any workaround?

Do not use .UseMauiCompatibility() if you need to support Windows platform.

Relevant log output

Microsoft Visual Studio Community 2022
Version 17.4.1
VisualStudio.17.Release/17.4.1+33110.190
Microsoft .NET Framework
Version 4.8.04084

Installed Version: Community

Visual C++ 2022   00482-90000-00000-AA019
Microsoft Visual C++ 2022

ASP.NET and Web Tools   17.4.326.54890
ASP.NET and Web Tools

Azure App Service Tools v3.0.0   17.4.326.54890
Azure App Service Tools v3.0.0

Azure Functions and Web Jobs Tools   17.4.326.54890
Azure Functions and Web Jobs Tools

C# Tools   4.4.0-6.22559.4+d7e8a398ef479a908e76bded82150c39251d0c9c
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools   1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Extensibility Message Bus   1.4.1 (main@2ee106a)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Mono Debugging for Visual Studio   17.4.19 (8c0a575)
Support for debugging Mono processes with Visual Studio.

NuGet Package Manager   6.4.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

Razor (ASP.NET Core)   17.0.0.2246202+61cc048d36a3fc9246d2f04625988b19a18ab8f0
Provides languages services for ASP.NET Core Razor.

SQL Server Data Tools   17.0.62207.28050
Microsoft SQL Server Data Tools

TypeScript Tools   17.0.10921.2001
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools   4.4.0-6.22559.4+d7e8a398ef479a908e76bded82150c39251d0c9c
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools   17.4.0-beta.22512.4+525d5109e389341bb90b144c24e2ad1ceec91e7b
Microsoft Visual F# Tools

Visual Studio IntelliCode   2.2
AI-assisted development for Visual Studio.

VisualStudio.DeviceLog   1.0
Information about my package

VisualStudio.Mac   1.0
Mac Extension for Visual Studio

VSPackage Extension   1.0
VSPackage Visual Studio Extension Detailed Info

Xamarin   17.4.0.301 (d17-4@96af3ae)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer   17.4.0.138 (remotes/origin/d17-4@d36bba3cc9)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates   17.4.2 (c457c97)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK   13.1.0.1 (d17-4/13ba222)
Xamarin.Android Reference Assemblies and MSBuild support.
    Mono: a96bde9
    Java.Interop: xamarin/java.interop/d17-4@fcc33ce2
    SQLite: xamarin/sqlite/3.39.3@23e1ae7
    Xamarin.Android Tools: xamarin/xamarin-android-tools/main@0be567a


Xamarin.iOS and Xamarin.Mac SDK   16.0.0.92 (3dd3dc52c)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-compatibilityIssues related to features in the Compatibility Nugetmigration-compatibilityXamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convertpartner/syncfusionIssues / PR's with Syncfusion collaborationplatform/windowss/duplicate 2️⃣This issue or pull request already existss/triagedIssue has been revieweds/try-latest-versionPlease try to reproduce the potential issue on the latest public versions/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions