Xamarin forms Only the original thread that created a view hierarchy can touch its views

0

Hello friends, I am receiving this error,

  

Android.Util.AndroidRuntimeException: Only the original thread that created a view of the hierarchy can touch its views.

After implementing the following in my code

public Comenatrios(int ID) {
  InitializeComponent();
  IdCom.Text = ID.ToString();
  IdCita = ID;
  GetComentarios(IdCita);

  //esta es la parte que implenté
  var periodTimeSpan2_ = TimeSpan.FromSeconds(4);

  var startTimeSpan = TimeSpan.Zero;
  var timer2_ = new System.Threading.Timer((e) => {
    GetComentarios(IdCita);
  }, null, startTimeSpan, periodTimeSpan2_);
}

// tambien intenté con esto, pero necesito que se corra ese metodo cada 4 o 5 segundos        
Device.BeginInvokeOnMainThread(() => {
  GetComentarios(IdCita); // este metodo es un simple get a un api
});
Or does someone know how I can do something similar to what I intend? In the sense that I need to run a method in a view while it is active.     
asked by E.Rawrdríguez.Ophanim 08.08.2018 в 00:56
source

1 answer

1

Try a Timer:

Device.StartTimer(TimeSpan.FromSeconds(5), () =>
{
    Device.BeginInvokeOnMainThread(() =>
    {
        GetComentarios(IdCita);
    });
    return true;
});
    
answered by 08.08.2018 / 10:25
source