Xamarin.Forms - UIKit Consistency error for iOS only

Ganesh Gebhard 366 Reputation points
2021-09-15T15:39:00.087+00:00

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

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,272 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ganesh Gebhard 366 Reputation points
    2021-09-22T01:13:52.34+00:00

    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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 25,341 Reputation points Microsoft Vendor
    2021-09-16T07:36:01.54+00:00

    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.

    0 comments No comments