简体   繁体   中英

Screen freezes when I click on a button(Xamarin.forms)

I'm trying to make a page where when I click on a button, it retrieves data and shows an activity indicator while it waits.

But when click on the button, it freezes. I binded the isVisible Property of the button and set him to false when clicked. When it's done, it sets the isVisible to true, but the screen still freezes.

And then i try with this code

Device.BeginInvokeOnMainThread (() => {
  VidljivoDugmePretrazi = false;
});

But didnt work, and the same situation on login page and work. Can someone help me, or have someone experience with this situation? Thank you.

You need to call your asynchronous method from the main thread and then update your isVisible Property in your viewmodel.

Device.BeginInvokeOnMainThread(YourAsyncMethod);
private async void YourAsyncMethod()
{
    //...
    VidljivoDugmePretrazi = true;
    try
    {
      await GetDataAsync();
    }
    catch (Exception e) 
    {
      //...
    }
    finally
    {
       VidljivoDugmePretrazi = false;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM