简体   繁体   English

单击按钮时屏幕冻结(Xamarin.forms)

[英]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.我绑定了按钮的 isVisible 属性,并在单击时将其设置为 false。 When it's done, it sets the isVisible to true, but the screen still freezes.完成后,它将 isVisible 设置为 true,但屏幕仍然冻结。

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.您需要从主线程调用异步方法,然后在视图模型中更新 isVisible 属性。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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