Error with a ProgressDialog Xamarin Android "Only the original thread that created a view hierarchy can touch its views"

0

I have a method, where I do a rest to bring a json, and then I deserialize it and put it into a DataGrid, until then everything is fine, now what I'm trying to do is fill in the data grid, put a progress dialog , but in doing so, this error marks me:

"Only the original thread that created a view of the hierarchy can touch its views"

The error marks me in the catch of the Rest method, but only marks it when the RelacionClientesRest () method I put it in the

 var progressDialog = ProgressDialog.Show(this, "Espere un momento", "Cargando registros", true);
            new Thread(new ThreadStart(delegate
            {
                RelacionClientesREST();

                RunOnUiThread(() => Toast.MakeText(this, "Toast within progress dialog.", ToastLength.Long).Show());

                RunOnUiThread(() => progressDialog.Hide());
            })).Start();

Why is it? This is my code:

 protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.RelacionClientes);
            ActionBar.SetDisplayHomeAsUpEnabled(true);

            var progressDialog = ProgressDialog.Show(this, "Espere un momento", "Cargando registros", true);
            new Thread(new ThreadStart(delegate
            {
                RelacionClientesREST();

                RunOnUiThread(() => Toast.MakeText(this, "Toast within progress dialog.", ToastLength.Long).Show());

                RunOnUiThread(() => progressDialog.Hide());
            })).Start();
        }

Here my Rest method

 public void RelacionClientesREST()
        {
            try
            {
                RestClient client = new RestClient("http://portalclientewa.azurewebsites.net/api/RelacionClientes/");
                var request = new RestRequest("GetData", Method.GET);
                request.Timeout = 1500000;
                request.RequestFormat = DataFormat.Json;
                request.AddParameter("idP", Idp);
                var temp = client.Execute(request).Content;
                var parsedJson = JsonConvert.DeserializeObject(temp).ToString();
                var lst = JsonConvert.DeserializeObject<List<ClientesProp>>(parsedJson).ToList();
                dataGrid.ItemsSource = lst;
            }
            catch (Exception ex)
            {
                Toast.MakeText(this, "No hay datos registrados", ToastLength.Short).Show();
            }
    
asked by Oscar Navarro 11.10.2017 в 18:40
source

1 answer

1

You are assigned the ItemSource of dataGrid in the thread that is not in the view. You have to execute the assignment in the thread of the view. Change:

var lst = JsonConvert.DeserializeObject<List<ClientesProp>>(parsedJson).ToList();
dataGrid.ItemsSource = lst;
//...

By:

var lst = JsonConvert.DeserializeObject<List<ClientesProp>>(parsedJson).ToList();
RunOnUIThread(()=>{
    dataGrid.ItemsSource = lst;
});  
//..
    
answered by 11.10.2017 / 19:19
source