question

JK-9346 avatar image
0 Votes"
JK-9346 asked WenyanZhang-MSFT commented

Xamarin iOS Navigation PushAsync/PushModalAsync and UI Thread

Hello Experts,
I have noticed that opening new modal or non-modal window in Xamarin Portable app (from ICommand binding in Xaml) behaves unstable - app freezes and Mac console shows "backboardd - invalid parameters message". Should this call (Navigation.PushModalAsync/Navigation.PushAsync) be done within UI thread (Device.BeginInvokeOnMainThread) or not? (XamarinForms 4.8.0.1821)
thanks for tips.
JK

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

WenyanZhang-MSFT avatar image
0 Votes"
WenyanZhang-MSFT answered WenyanZhang-MSFT commented

Hello,

Should this call (Navigation.PushModalAsync/Navigation.PushAsync) be done within UI thread (Device.BeginInvokeOnMainThread) or not?

Yes. UI changes should be made in main thread(UI thread).

Most operating systems — including iOS, Android, and the Universal Windows Platform — use a single-threading model for code involving the user interface. This model is necessary to properly serialize user-interface events, including keystrokes and touch input. This thread is often called the main thread or the user-interface thread or the UI thread. The disadvantage of this model is that all code that accesses user interface elements must run on the application's main thread.


For more details, refer to
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/ios/main-thread-updates-ui
https://docs.microsoft.com/en-us/xamarin/essentials/main-thread?context=xamarin%2Fxamarin-forms


--------UPDATE------

So when I call Task.Run to load some data from service this will not be working simultaneously with UI thread?

Task.Run method queues the specified work to run on the ThreadPool, not the main thread. Refer to https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.run?view=net-6.0

can InitializeComponent on a large Xaml (even precompiled) cause app hang?

You could try to check if you can catch the exception and check if there is any ex message.

    try
         {
             InitializeComponent();
         }
         catch (Exception ex)
         {
             throw ex;
         }

As @JK-9346 said, "Actually the exception was not thrown but I noticed that OnSizeAllocated was called while InitializeComponent and the code inside this event handler should not be executed because xaml was not yet ready. The conclusion is that we should not change sizes of xaml controls until it is loaded - also make sure the code does not cause any loop."

For the other two assembly loading questions, try to check XAML Compilation in Xamarin.Forms and Loading XAML at Runtime in Xamarin.Forms

Best Regards,
Wenyan Zhang


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

So when I call Task.Run to load some data from service this will not be working simultaneously with UI thread? And one more possibility - can InitializeComponent on a large Xaml (even precompiled) cause app hang? I see that xaml size can be the reason of this random hangs. How is precompiled xaml loaded into memory? Is it possible that xaml is loaded asynchronously and form being created needs to wait? I do not understand the architecture of portable assembly loading. Looks like wjhen I wait longer the hangs do not occur

0 Votes 0 ·

I have edited the answer, you could check it.

0 Votes 0 ·
JK-9346 avatar image JK-9346 WenyanZhang-MSFT ·

OK problem solved - while executing InitializeComponent certain events as OnSizeAllocated are fired so when there is some code manipulating the layout this may randomly fail. This code should be active after the view has been created. Now everything seems to work. Thank you for advices.

0 Votes 0 ·
Show more comments