dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.04k stars 1.73k forks source link

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

Open holecekp opened 1 year ago

holecekp commented 1 year ago

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.
ghost commented 1 year ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

holecekp commented 1 year ago

This should not have the label "migration-compatibility" because a new MAUI project crashes and no compatibility features are used in it! Just a good old button, label and a grid. I have found out that UseMauiCompatibility() can be used as a workaround for some of the simpler cases but not all of them as you can see in the issue description.

The issue has been moved directly to backlog so a crashing app is considered to be a new standard for Microsoft? Moreover this is a regression issue. It did not crash in .NET 6 but it started to crash after upgrade to .NET 7 (although .NET 6 was also not perfect because the lazy-view was not loaded but at least the app would not crash). Would not be it beneficial to solve it right now when you know which changes have been made recently that could cause it?

NonameMissingNo commented 1 year ago

image When is it supposed to crash? I've been pressing next 12 times, and there has been no crash after I removed the UseMauiCompatibility from the MauiProgram.cs

What version of Windows are you using? What version of Maui are you using?

ghost commented 1 year ago

Hi @holecekp. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

holecekp commented 1 year ago

@NonameMissingNo I am using 21H2 (build 9044.2251). I have cloned my repro project again into a different directory and run it from there and I can confirm that it is still crashing. So this rules out the possibility that the crash would be caused by some temporary or other project file that would not be saved into Git. I have noticed that you are using the dark mode. I am using the light theme so I switched to the dark theme and tested the app but it is still crashing.

I am using the latest VS (17.4.1) and the latest .NET 7 MAUI (7.0.49). I have tested it also with the latest .NET 6 version of MAUI (6.0.547) - although it does not crash in .NET 6, it does not work correctly because the "test" text is not displayed after clicking the button. Also, all Nugets are up-to-date.

NonameMissingNo commented 1 year ago

As a quick theory, it's possible that the problem is the 21H2 (I'm using the insider build). Quick update: But Net 6, and Net 7 has pressable Next buttons, and are writing test.

holecekp commented 1 year ago

I have also installed the latest VS 2022 Preview (17.5.0 Preview 1.0) and tested it and the behavior on my computer is the same as with the stable version of VS 2022 (17.4.1) - crash in .NET 7 and "test" not displayed in .NET 6. So I can rule out that the problem is caused by a wrong VS installation.

In case it is helpful, I am including the full stack trace for the crash but I cannot tell from it much. The crash happens when ContentView.Content is modified (the TestView with the "test" Label is loaded into it):

System.InvalidOperationException: The handler's MauiContext cannot be null.
   at Microsoft.Maui.Handlers.ViewHandler.MapContextFlyout(IElementHandler handler, IContextFlyoutElement contextFlyoutContainer) in Microsoft.Maui.Handlers\ViewHandler.cs:line 471
   at Microsoft.Maui.Handlers.ViewHandler.MapContextFlyout(IViewHandler handler, IView view) in Microsoft.Maui.Handlers\ViewHandler.cs:line 463
   at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView) in Microsoft.Maui\PropertyMapper.cs:line 87
   at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view) in Microsoft.Maui.Handlers\ElementHandler.cs:line 82
   at Microsoft.Maui.Handlers.ViewHandler`2.SetVirtualView(IView view) in Microsoft.Maui.Handlers\ViewHandler.cs:line 555
   at Microsoft.Maui.Controls.Compatibility.RendererToHandlerShim.SetVirtualView(IView view) in Microsoft.Maui.Controls.Compatibility\RendererToHandlerShim.cs:line 152
   at Microsoft.Maui.Handlers.ViewHandler`2.SetVirtualView(IElement view) in Microsoft.Maui.Handlers\ViewHandler.cs:line 560
   at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in Microsoft.Maui.Controls\Element.cs:line 974
   at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in Microsoft.Maui.Controls\Element.cs:line 425
   at Microsoft.Maui.Controls.VisualElement.set_Handler(IViewHandler value) in Microsoft.Maui.Controls\VisualElement.cs:line 316
   at Microsoft.Maui.Controls.Compatibility.RendererToHandlerShim.SetupRenderer(IVisualElementRenderer visualElementRenderer) in Microsoft.Maui.Controls.Compatibility\RendererToHandlerShim.cs:line 78
   at Microsoft.Maui.Controls.Compatibility.RendererToHandlerShim..ctor(IVisualElementRenderer visualElementRenderer) in Microsoft.Maui.Controls.Compatibility\RendererToHandlerShim.cs:line 69
   at Microsoft.Maui.Controls.Compatibility.Platform.UWP.Platform.<>c.<.cctor>b__58_0(BindableObject bindable, Object oldvalue, Object newvalue) in Microsoft.Maui.Controls.Compatibility.Platform.UWP\Platform.cs:line 34
   at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes) in Microsoft.Maui.Controls\BindableObject.cs:line 630
   at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value, Boolean fromStyle, Boolean checkAccess) in Microsoft.Maui.Controls\BindableObject.cs:line 531
   at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value) in Microsoft.Maui.Controls\BindableObject.cs:line 497
   at Microsoft.Maui.Controls.Compatibility.Platform.UWP.Platform.SetRenderer(VisualElement element, IVisualElementRenderer value) in Microsoft.Maui.Controls.Compatibility.Platform.UWP\Platform.cs:line 73
   at Microsoft.Maui.Controls.Compatibility.Platform.UWP.VisualElementPackager.SetupVisualElement(VisualElement view) in Microsoft.Maui.Controls.Compatibility.Platform.UWP\VisualElementPackager.cs:line 130
   at Microsoft.Maui.Controls.Compatibility.Platform.UWP.VisualElementPackager.OnChildAdded(Object sender, ElementEventArgs e) in Microsoft.Maui.Controls.Compatibility.Platform.UWP\VisualElementPackager.cs:line 155
   at Microsoft.Maui.Controls.Element.OnChildAdded(Element child) in Microsoft.Maui.Controls\Element.cs:line 550
   at Microsoft.Maui.Controls.VisualElement.OnChildAdded(Element child) in Microsoft.Maui.Controls\VisualElement.cs:line 1750
   at Microsoft.Maui.Controls.Compatibility.Layout.OnInternalAdded(View view) in Microsoft.Maui.Controls.Compatibility\Layout.cs:line 548
   at Microsoft.Maui.Controls.Compatibility.Layout.InternalChildrenOnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) in Microsoft.Maui.Controls.Compatibility\Layout.cs:line 529
   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) in /_/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:line 192
   at Microsoft.Maui.Controls.TemplateUtilities.OnContentChanged(BindableObject bindable, Object oldValue, Object newValue) in Microsoft.Maui.Controls\TemplateUtilities.cs:line 75
   at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes) in Microsoft.Maui.Controls\BindableObject.cs:line 630
   at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value, Boolean fromStyle, Boolean checkAccess) in Microsoft.Maui.Controls\BindableObject.cs:line 531
   at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value) in Microsoft.Maui.Controls\BindableObject.cs:line 497
   at Microsoft.Maui.Controls.ContentView.set_Content(View value) in Microsoft.Maui.Controls\ContentView.cs:line 17
   at MauiBugCrashInLazyView.LazyView`1.LoadView() in D:\dotNET\MauiBugCrashInLazyView\MauiBugCrashInLazyView\LazyView.cs:line 51
   at MauiBugCrashInLazyView.VisibilityLazyView`1.LoadView() in D:\dotNET\MauiBugCrashInLazyView\MauiBugCrashInLazyView\LazyView.cs:line 32
   at MauiBugCrashInLazyView.VisibilityLazyView`1.EnsureLoadedIfVisible() in D:\dotNET\MauiBugCrashInLazyView\MauiBugCrashInLazyView\LazyView.cs:line 26
   at MauiBugCrashInLazyView.VisibilityLazyView`1.OnPropertyChanged(String propertyName) in D:\dotNET\MauiBugCrashInLazyView\MauiBugCrashInLazyView\LazyView.cs:line 14
   at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent) in Microsoft.Maui.Controls\BindableObject.cs:line 681
   at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes) in Microsoft.Maui.Controls\BindableObject.cs:line 630
   at Microsoft.Maui.Controls.BindingExpression.ApplyCore(Object sourceObject, BindableObject target, BindableProperty property, Boolean fromTarget) in Microsoft.Maui.Controls\BindingExpression.cs:line 364
   at Microsoft.Maui.Controls.BindingExpression.Apply(Boolean fromTarget) in Microsoft.Maui.Controls\BindingExpression.cs:line 274
   at Microsoft.Maui.Controls.BindingExpression.BindingExpressionPart.<PropertyChanged>b__49_0() in Microsoft.Maui.Controls\BindingExpression.cs:line 188
   at Microsoft.Maui.Controls.DispatcherExtensions.DispatchIfRequired(IDispatcher dispatcher, Action action) in Microsoft.Maui.Controls\DispatcherExtensions.cs:line 66
   at Microsoft.Maui.Controls.BindingExpression.BindingExpressionPart.PropertyChanged(Object sender, PropertyChangedEventArgs args) in Microsoft.Maui.Controls\BindingExpression.cs:line 185
   at Microsoft.Maui.Controls.BindingExpression.WeakPropertyChangedProxy.OnPropertyChanged(Object sender, PropertyChangedEventArgs e) in Microsoft.Maui.Controls\BindingExpression.cs:line 92
   at MauiBugCrashInLazyView.TestViewModel.OnPropertyChanged(String propertyName) in D:\dotNET\MauiBugCrashInLazyView\MauiBugCrashInLazyView\TestViewModel.cs:line 38
   at MauiBugCrashInLazyView.TestViewModel.set_ShowView(Boolean value) in D:\dotNET\MauiBugCrashInLazyView\MauiBugCrashInLazyView\TestViewModel.cs:line 24
   at MauiBugCrashInLazyView.TestViewModel.LoadView() in D:\dotNET\MauiBugCrashInLazyView\MauiBugCrashInLazyView\TestViewModel.cs:line 31
   at Microsoft.Maui.Controls.Command.<>c__DisplayClass4_0.<.ctor>b__0(Object o) in Microsoft.Maui.Controls\Command.cs:line 89
   at Microsoft.Maui.Controls.Command.Execute(Object parameter) in Microsoft.Maui.Controls\Command.cs:line 135
   at Microsoft.Maui.Controls.ButtonElement.ElementClicked(VisualElement visualElement, IButtonElement ButtonElementManager) in Microsoft.Maui.Controls\ButtonElement.cs:line 65
   at Microsoft.Maui.Controls.Button.SendClicked() in Microsoft.Maui.Controls\Button.cs:line 426
   at Microsoft.Maui.Controls.Button.Microsoft.Maui.IButton.Clicked() in Microsoft.Maui.Controls\Button.cs:line 630
   at Microsoft.Maui.Handlers.ButtonHandler.OnClick(Object sender, RoutedEventArgs e) in Microsoft.Maui.Handlers\ButtonHandler.cs:line 144
   at WinRT._EventSource_global__Microsoft_UI_Xaml_RoutedEventHandler.EventState.<GetEventInvoke>b__1_0(Object sender, RoutedEventArgs e)
   at ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e)
--- End of stack trace from previous location ---
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
   at ABI.Microsoft.UI.Xaml.Controls.IControlOverridesMethods.OnPointerReleased(IObjectReference _obj, PointerRoutedEventArgs e)
   at Microsoft.UI.Xaml.Controls.Control.OnPointerReleased(PointerRoutedEventArgs e)
   at Microsoft.UI.Xaml.Controls.Control.Microsoft.UI.Xaml.Controls.IControlOverrides.OnPointerReleased(PointerRoutedEventArgs e)
   at ABI.Microsoft.UI.Xaml.Controls.IControlOverrides.Do_Abi_OnPointerReleased_3(IntPtr thisPtr, IntPtr e)
holecekp commented 1 year ago

@NonameMissingNo I have installed VS 2022 Community to another computer and cloned my repro project and it is also crashing on this other computer. This means that the problem is not just in my computer (for example, corrupted VS installation) but it is a general problem. Both computer have Windows 10 21H2 version (build 19044.2251) so it supports the theory that MAUI app could have problems with this particular version.

drasticactions commented 1 year ago

@holecekp

I think you have it backwards, the crash happens when you have UseMauiCompatability enabled, and works correctly when it's disabled. Specifically, here, https://github.com/dotnet/maui/blob/9e8144915e59a7a0c2b7875170a3b8e12b235b68/src/Core/src/Handlers/View/ViewHandler.Windows.cs#L115

This is looking for the Handler, RendererToHandlerShim:

https://github.com/dotnet/maui/blob/9ee62c1b125424ea54c99661de8be951f21203a3/src/Compatibility/Core/src/RendererToHandlerShim.cs#L122

That's used when you have UseMauiCompatability enabled. The bug is that when the compatibility layer is used, and LazyView calls to create the view (https://github.com/holecekp/MauiBugCrashInLazyView/blob/main/MauiBugCrashInLazyView/LazyView.cs#L50),there should be a call in the VisualElementRenderer via OnElementUpdated to call on the Renderer shim to add the Maui Context. That never fires, so the context never gets set. Most likely, as far as I can tell, that's due to that the assumption that the ContentView is part of a Page, which would cause the VisualElementRenderer to fire that request.

If you turn off UseMauiCompatability, that code is never called and the MauiContext is set up correctly in the handlers.

holecekp commented 1 year ago

@drasticactions You are right. I do not know how it is possible but I must have switched the version with and without UseMauiCompatability. I am sorry for the confusion. I have tested it again and it is exactly as you described - it crashes with UseMauiCompatability enabled and it works without UseMauiCompatability.

jsuarezruiz commented 1 year ago

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 absence of UseMauiCompatibility() in MauiProgram even though no legacy features or controls are used! I thought that this UseMauiCompatibility() is optional and that it has impact on performance. Moreover, this holds only for the simple repro project. The larger project from which the repro project was derived crashes even with UseMauiCompatibility().

I think that it worked in the previous .NET 6 version but I am not sure. At least it did not crash in my project.

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. Add 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?

Adding .UseMauiCompatibility() in MauiProgram can help for some project. Although it helps for the repro project, it does not help for my full project. So it is not a universal remedy. This will probably have also some performance impact.

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.

Could you attach the sample here?, the repo seems to no longer exists.

holecekp commented 1 year ago

I have uploaded the repo again: https://github.com/holecekp/MauiBugCrashInLazyView

XamlTest commented 1 year ago

Verified this on Visual Studio Enterprise 17.7.0 Preview 1.0. Repro on Windows 11 with above provided Project: MauiBugCrashInLazyView.zip

image

MitchBomcanhao commented 8 months ago

I've stumbled into a similar situation - this is now a blocker.

JTOne123 commented 4 months ago

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 absence of UseMauiCompatibility() in MauiProgram even though no legacy features or controls are used! I thought that this UseMauiCompatibility() is optional and that it has impact on performance. Moreover, this holds only for the simple repro project. The larger project from which the repro project was derived crashes even with UseMauiCompatibility(). I think that it worked in the previous .NET 6 version but I am not sure. At least it did not crash in my project. 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. Add 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?

Adding .UseMauiCompatibility() in MauiProgram can help for some project. Although it helps for the repro project, it does not help for my full project. So it is not a universal remedy. This will probably have also some performance impact.

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.

Could you attach the sample here?, the repo seems to no longer exists.

@jsuarezruiz Here you go

Please use UseMauiCompatibility and try use this control, it throws the same error.

    public class CustomControl : ContentView
    {
        public CustomControl()
        {
            Content = new ContentView();
        }
    }
JTOne123 commented 4 months ago

https://github.com/dotnet/maui/issues/13282

JTOne123 commented 4 months ago

Possible workaround is just adding the next line to your windows handlers handlers.AddHandler<ContentView, ContentViewHandler>();

zenith6495 commented 3 months ago

any updates on this issue? still facing the same issue when UseMauiCompatibility is enabled