question

GaneshGebhard-8778 avatar image
0 Votes"
GaneshGebhard-8778 asked GaneshGebhard-8778 answered

Xamarin.Forms - UIKit Consistency error for iOS only

Hey!

I have a C# class file where I navigate to other pages, depending on the input. Since the interface is something different than a page, I have to invoke page navigation with InvokeOnMainThreadAsync. I did this with the following method:

 public void Page_Navigation(Page navigationpage)
     {
         Device.InvokeOnMainThreadAsync(async () =>
         {
             if (navigationpage == null)
             {
                 await Navigation.PopAsync();
             }
             else
             {
                 var currentPage = Navigation.NavigationStack.LastOrDefault();
                 await Navigation.PushAsync(navigationpage);
                 Navigation.RemovePage(currentPage);
             }
         });
     }

Calling the method looks like this:

132463-gtyl5.png

I test everything on Android, but when I'm done I also test it on iOS just to be sure. On Android everything works, but on iOS I get the following error when I call with method:

"UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread."

That something like this occurs is one thing, but that it only happens on iOS is something else. Does anyone know what is wrong here and how to fix it?

Best


dotnet-xamarinforms
gtyl5.png (9.0 KiB)
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.

GaneshGebhard-8778 avatar image
0 Votes"
GaneshGebhard-8778 answered

I've contacted Microsoft for this and it turns out this is a problem in VS itself. I installed an updated version of my app on an IPhone with App Center and everything works just fine.

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.

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

Hello,
Welcome to our Microsoft Q&A platform!

Accessing user interface elements must run on the application's main thread. I create two pages to test this issue, but I 'm so sorry I can't reproduce it. The following code shows how I did , you can check it.

1.create two pages and add a button at first page. When I click the button, I try to call the Page_Navigation method you provided.
2.use Xamarin.Essentials: MainThread to get MainThread .

Both these two ways did not reproduce the issue, I'm afraid you could check your calling method or make break point to determine which line of the codes post this error message or share more information about the calling mothod. You also could upload your basic, minimal, reproducible project to github and attach the link here, then I will download and test it.

  void Button_Clicked(System.Object sender, System.EventArgs e)
     {
         if (MainThread.IsMainThread) {
             Page_Navigation(null);
    
             //Page_Navigation(new MyPage());
         }
         else
         {
             Console.WriteLine("not main");
         }
     }
     public void Page_Navigation(Page navigationpage)
     {
         MainThread.BeginInvokeOnMainThread(async () =>
         {
             if (navigationpage == null)
             {
                 await Navigation.PopAsync();
             }
             else
             {
                 var currentPage = Navigation.NavigationStack.LastOrDefault();
                 await Navigation.PushAsync(navigationpage);
                 Navigation.RemovePage(currentPage);
             }
         });
         //Device.InvokeOnMainThreadAsync(async () =>
         //{
         //    if (navigationpage == null)
         //    {
         //        await Navigation.PopAsync();
         //    }
         //    else
         //    {
         //        var currentPage = Navigation.NavigationStack.LastOrDefault();
         //        await Navigation.PushAsync(navigationpage);
         //        Navigation.RemovePage(currentPage);
         //    }
         //});
     }  


Best Regards,
Wenyan Zhang



If the response is helpful, please click "Accept Answer" and upvote it.
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.



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.