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();
}