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.01k stars 1.73k forks source link

Error when using DisplayAlert in override void onAppearing #12739

Closed JDanBankai closed 1 year ago

JDanBankai commented 1 year ago

Description

Hi Team,

I'm actually moving my app from xamarin to .net maui.

In one of my app page. An alert is displayed when the screen is loaded.

Using the protected override void OnAppearing().

But when I do the same in .net maui, the app crash before the page is loaded.

If I removed the await DisplayAlert part, the page is loaded correctly.

Below sample code:

ASP.NET (C#)

protected override async void OnAppearing() { base.OnAppearing();

        NetworkAccess accessType = Connectivity.Current.NetworkAccess;

        if (accessType == NetworkAccess.Internet)
        {
            bool answer = await DisplayAlert("Connectivity Info", "You do not have internet connection" + "\n" + "Show your Offline Card??", "Yes", "No");

            if (answer == true)
            {
                await Shell.Current.GoToAsync(state: "OfflineCard");
            }
        }
    }

Steps to Reproduce

  1. Create a File > New .NET MAUI App
  2. Override the OnAppearing
  3. Add a DisplayAlert after base.OnAppearing();

Expected outcome: An alert is display when the page is loaded. Actual outcome: the app crash before the page is loaded.

Link to public reproduction project repository

https://github.com/dotnet/maui/blob/main/.github/repro.md

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, Windows, macOS

Affected platform versions

All

Did you find any workaround?

No

Relevant log output

'MauiApp3.exe' (CoreCLR: DefaultDomain): Loaded 'C:\Users\DDESIRE\source\repos\MauiApp3\MauiApp3\bin\Debug\net7.0-windows10.0.19041.0\win10-x64\AppX\System.Private.CoreLib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MauiApp3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\DDESIRE\source\repos\MauiApp3\MauiApp3\bin\Debug\net7.0-windows10.0.19041.0\win10-x64\AppX\MauiApp3.dll'. Symbols loaded.
'MauiApp3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\DDESIRE\source\repos\MauiApp3\MauiApp3\bin\Debug\net7.0-windows10.0.19041.0\win10-x64\AppX\System.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MauiApp3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\DDESIRE\source\repos\MauiApp3\MauiApp3\bin\Debug\net7.0-windows10.0.19041.0\win10-x64\AppX\Microsoft.Windows.ApplicationModel.WindowsAppRuntime.Projection.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MauiApp3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\DDESIRE\source\repos\MauiApp3\MauiApp3\bin\Debug\net7.0-windows10.0.19041.0\win10-x64\AppX\System.Runtime.InteropServices.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MauiApp3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\DDESIRE\source\repos\MauiApp3\MauiApp3\bin\Debug\net7.0-windows10.0.19041.0\win10-x64\AppX\WinRT.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MauiApp3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\DDESIRE\source\repos\MauiApp3\MauiApp3\bin\Debug\net7.0-windows10.0.19041.0\win10-x64\AppX\System.Collections.Concurrent.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MauiApp3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\DDESIRE\source\repos\MauiApp3\MauiApp3\bin\Debug\net7.0-windows10.0.19041.0\win10-x64\AppX\System.Collections.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MauiApp3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\DDESIRE\source\repos\MauiApp3\MauiApp3\bin\Debug\net7.0-windows10.0.19041.0\win10-x64\AppX\System.Threading.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MauiApp3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\DDESIRE\source\repos\MauiApp3\MauiApp3\bin\Debug\net7.0-windows10.0.19041.0\win10-x64\AppX\System.Runtime.CompilerServices.Unsafe.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MauiApp3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\DDESIRE\source\repos\MauiApp3\MauiApp3\bin\Debug\net7.0-windows10.0.19041.0\win10-x64\AppX\System.Private.Uri.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MauiApp3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\DDESIRE\source\repos\MauiApp3\MauiApp3\bin\Debug\net7.0-windows10.0.19041.0\win10-x64\AppX\System.ObjectModel.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MauiApp3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\DDESIRE\source\repos\MauiApp3\MauiApp3\bin\Debug\net7.0-windows10.0.19041.0\win10-x64\AppX\System.ComponentModel.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MauiApp3.exe' (CoreCLR: clrhost): Loaded 'C:\Users\DDESIRE\source\repos\MauiApp3\MauiApp3\bin\Debug\net7.0-windows10.0.19041.0\win10-x64\AppX\System.Numerics.Vectors.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The program '[21732] MauiApp3.exe' has exited with code 2147942405 (0x80070005).
JDanBankai commented 1 year ago

Link to public reproduction project repository https://github.com/JDanBankai/DisplayAlertError

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.

shadowfoxish commented 1 year ago

Some potentially useful information; (I was able to run your project and encounter the same issue with DisplayAlert)

Time    Device Name Type    PID Tag Message
01-23 11:06:28.122  Datalogic Memor K   Warning 698 System.err  java.lang.Throwable
    at com.mediatek.server.wifi.MtkWifiService$AutoConnectManager.setDisconnectOperation(MtkWifiService.java:340)
    at com.mediatek.server.wifi.MtkWifiService$AutoConnectManager.access$300(MtkWifiService.java:206)
    at com.mediatek.server.wifi.MtkWifiService$MtkWifiOpReceiver.onReceive(MtkWifiService.java:170)
    at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0(LoadedApk.java:1432)
    at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at com.android.server.SystemServer.run(SystemServer.java:502)
    at com.android.server.SystemServer.main(SystemServer.java:329)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:891)

The stack trace is a bit different in visual studio when it traps it

System.Reflection.TargetInvocationException
    0xFFFFFFFFFFFFFFFF in Android.Runtime.JNIEnv.monodroid_debugger_unhandled_exception C#
    0x1A in Android.Runtime.JNINativeWrapper._unhandled_exception at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12,5 C#
    0x23 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:353,26    C#
    0x32 in System.Reflection.MethodInvoker.InterpretedInvoke   C#
    0x4 in System.Reflection.MethodInvoker.InlinedInvoke    C#
    0xCE in System.Reflection.RuntimeMethodInfo.Invoke  C#
    0x6 in System.Reflection.MethodBase.Invoke  C#
    0x95 in Microsoft.Maui.Controls.MessagingCenter.Subscription.InvokeCallback at D:\a\_work\1\s\src\Controls\src\Core\MessagingCenter.cs:97,5 C#
    0x70 in Microsoft.Maui.Controls.MessagingCenter.InnerSend at D:\a\_work\1\s\src\Controls\src\Core\MessagingCenter.cs:220,6  C#
    0x35 in Microsoft.Maui.Controls.MessagingCenter.Microsoft.Maui.Controls.IMessagingCenter.Send<Microsoft.Maui.Controls.Page, Microsoft.Maui.Controls.Internals.AlertArguments> at D:\a\_work\1\s\src\Controls\src\Core\MessagingCenter.cs:118,4  C#
    0x8 in Microsoft.Maui.Controls.MessagingCenter.Send<Microsoft.Maui.Controls.Page, Microsoft.Maui.Controls.Internals.AlertArguments> at D:\a\_work\1\s\src\Controls\src\Core\MessagingCenter.cs:111,4    C#
    0x11 in Microsoft.Maui.Controls.Page. at D:\a\_work\1\s\src\Controls\src\Core\Page.cs:242,31    C#
    0x33 in Microsoft.Maui.Controls.Page.FlushPendingActions at D:\a\_work\1\s\src\Controls\src\Core\Page.cs:270,6  C#
    0x15 in Microsoft.Maui.Controls.Page.SendNavigatedTo at D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Page.Impl.cs:46,4  C#
    0x6C in Microsoft.Maui.Controls.Shell.SendNavigated at D:\a\_work\1\s\src\Controls\src\Core\Shell\Shell.cs:1166,4   C#
    0x2 in Microsoft.Maui.Controls.Shell.<.ctor>b__172_0 at D:\a\_work\1\s\src\Controls\src\Core\Shell\Shell.cs:803,49  C#
    0x1D in Microsoft.Maui.Controls.ShellNavigationManager. at D:\a\_work\1\s\src\Controls\src\Core\Shell\ShellNavigationManager.cs:274,6   C#
    0x12 in Microsoft.Maui.Controls.ShellNavigationManager. at D:\a\_work\1\s\src\Controls\src\Core\Shell\ShellNavigationManager.cs:264,7   C#
    0x22 in Microsoft.Maui.Controls.BaseShellItem.OnAppearing at D:\a\_work\1\s\src\Controls\src\Core\Shell\BaseShellItem.cs:168,5  C#
    0x136 in Microsoft.Maui.Controls.ShellNavigationManager.HandleNavigated at D:\a\_work\1\s\src\Controls\src\Core\Shell\ShellNavigationManager.cs:262,6   C#
    0x6D in Microsoft.Maui.Controls.ShellNavigationManager. at D:\a\_work\1\s\src\Controls\src\Core\Shell\ShellNavigationManager.cs:243,7   C#
    0x65 in Microsoft.Maui.Controls.Element.OnChildAdded at D:\a\_work\1\s\src\Controls\src\Core\Element.cs:357,3   C#
    0x2 in Microsoft.Maui.Controls.ShellContent.OnChildAdded at D:\a\_work\1\s\src\Controls\src\Core\Shell\ShellContent.cs:153,4    C#
    0x28 in Microsoft.Maui.Controls.BaseShellItem.AddLogicalChild at D:\a\_work\1\s\src\Controls\src\Core\Shell\BaseShellItem.cs:274,4  C#
    0x30 in Microsoft.Maui.Controls.ShellContent.set_ContentCache at D:\a\_work\1\s\src\Controls\src\Core\Shell\ShellContent.cs:192,6   C#
    0x84 in Microsoft.Maui.Controls.ShellContent.Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent at D:\a\_work\1\s\src\Controls\src\Core\Shell\ShellContent.cs:81,5  C#
    0x16E in Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.OnCreateView at D:\a\_work\1\s\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellSectionRenderer.cs:123,5    C#
    0x24 in AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_ at /Users/runner/work/1/s/generated/androidx.fragment.fragment/obj/Release/net6.0-android/generated/src/AndroidX.Fragment.App.Fragment.cs:2031,4 C#
    0xD in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:352,5  C#

I suspect the splash screen is still disposing at the time the main screen is showing or just something in general isn't ready for the kind of interaction you're asking for, so on a hunch I tried adding a delay before doing the Display Alert thing, and then the popup showed. This works. But, this isn't a viable solution or workaround, but it may help the team with debugging or understanding where the problem is:

    public async Task CheckInternetConnectivity()
    {
        NetworkAccess accessType = Connectivity.Current.NetworkAccess;

        if (accessType == NetworkAccess.Internet)
        {
            await MainThread.InvokeOnMainThreadAsync(async () =>
            {
                bool answer = await DisplayAlert("Connectivity Info", "You do not have internet connection" + "\n" + "Show your Offline Card??", "Yes", "No");

                if (answer == true)
                {
                    await Shell.Current.GoToAsync(state: "OfflineCard");
                }
            });
        }
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        Task.Run(async () => { await Task.Delay(5000); await CheckInternetConnectivity(); }) ;
    }
JDanBankai commented 1 year ago

Hi Shadow, thanks for the work around.

I will try it and keep you posted.

jsuarezruiz commented 1 year ago

Could be fixed by https://github.com/dotnet/maui/pull/12910

jsuarezruiz commented 1 year ago

Is already working on main branch image

maza70 commented 1 year ago

I had the same Problem and use then the event Loaded on the first StackLayout, then the DisplayAlert works.

   <StackLayout  

                  HorizontalOptions="FillAndExpand"
                  VerticalOptions="FillAndExpand"
                  Style="{StaticResource BackgroundStyle}"
                  Loaded="StackLayout_Loaded"
            >
JDanBankai commented 1 year ago

Hi Team,

Thanks @jsuarezruiz for the info. I have tested it and it is now working correctly.

Closing the ticket.

cdavidyoung commented 1 year ago

I am not so sure this is fixed. I had the exact same problem with DisplayAlert during OnAppearing and adding the delay before it fixed the problem. I only noticed this happening on Windows but the fix applies to all 4 platforms.

ghost commented 1 year ago

Hello lovely human, thank you for your comment on this issue. Because this issue has been closed for a period of time, please strongly consider opening a new issue linking to this issue instead to ensure better visibility of your comment. Thank you!