xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

[Bug] Shell: First navigation call on background thread throws the exception "Can only be called on ui thread!" #14366

Open MaxFmi opened 3 years ago

MaxFmi commented 3 years ago

The first navigation call end up in this else case: https://github.com/xamarin/Xamarin.Forms/blob/ae8e52feb5faacf1890b870eb291b509e5b832e6/Xamarin.Forms.Core/Shell/ShellNavigationManager.cs#L166 Which is not surrounded by "InvokeOnMainThread" and throws the exception "Can only be called on ui thread!".
The navigation calls above are wrapped by "InvokeOnMainThread" and work as expected: https://github.com/xamarin/Xamarin.Forms/blob/ae8e52feb5faacf1890b870eb291b509e5b832e6/Xamarin.Forms.Core/Shell/ShellNavigationManager.cs#L160

Description

Steps to Reproduce

  1. Set MainPage of your Shell application
  2. Invoke navigation through "await Shell.Current.GoToAsync(route);"
  3. We end up in line 166 of ShellNavigationManager

Expected Behavior

Should navigate as expected.

Actual Behavior

Exception get thrown: "Can only be called on ui thread!"

Basic Information

Workaround

Wrap "await Shell.Current.GoToAsync(route);" in "await Device.InvokeOnMainThreadAsync(...);"

jfversluis commented 3 years ago

Reverted the PR for this as it caused an NRE in another scenario. At least this bug has a workaround. We should see if we can fix this properly.

jfversluis commented 3 years ago

@AlleSchonWeg

https://github.com/xamarin/Xamarin.Forms/pull/14730#issuecomment-943202026

Moving the discussion to here :)

Not sure if that's the issue. The actual exception happened on this line

https://github.com/xamarin/Xamarin.Forms/blob/7958ff904a0ca058fb9c76915aaeb4f6c7ef9a54/Xamarin.Forms.Core/Shell/ShellNavigationManager.cs#L253

Where element would be null