Error: Only the original thread that created a view hierarchy can touch its views in Xamarin

后端 未结 3 921
离开以前
离开以前 2021-01-20 05:54

I am getting an error error: Only the original thread that created a view hierarchy can touch its views

in line

BookingAdapter expListAd         


        
相关标签:
3条回答
  • 2021-01-20 06:07

    Here is a cross platform solution in xamarin for this issue;

     Device.BeginInvokeOnMainThread(() =>
     {
       //Your code here
     });
    
    0 讨论(0)
  • 2021-01-20 06:17

    You're calling GetServicesForUserCompleted method from SalonServicesClient on worker thread. Invoke it on the UI thread instead.

    0 讨论(0)
  • 2021-01-20 06:19

    The linked article in the accepted answer talks about InvokeOnMainThread which is an IOS thing. No idea why it was accepted as an answer for Android as the question is tagged.

    For Android you use Activity.RunOnUiThread. Relevant documentation is here: http://developer.xamarin.com/guides/android/advanced_topics/writing_responsive_applications/

    Because you are rigging this up from somewhere that inherits from Activity you can just wrap the relevant code so this:

    BookingAdapter expListAdapter = new BookingAdapter (this, listDataHeader,  listDataChild);
    try{
        explistView.SetAdapter (expListAdapter);
        explistView.SetGroupIndicator (null);
    }
    catch(Exception e) {
        Toast.MakeText (this,e+"",ToastLength.Long).Show();   
    }
    

    becomes:

    RunOnUiThread(() =>
    {
        BookingAdapter expListAdapter = new BookingAdapter (this, listDataHeader,  listDataChild);
        try{
            explistView.SetAdapter (expListAdapter);
            explistView.SetGroupIndicator (null);
        }
        catch(Exception e) {
            Toast.MakeText (this,e+"",ToastLength.Long).Show();   
        }
    });
    

    That's really all there is to it.

    0 讨论(0)
提交回复
热议问题