【问题标题】:Xamarin Forms - Lagging ScrollView after updating UI with HTTP POST Request ResultsXamarin Forms - 使用 HTTP POST 请求结果更新 UI 后滞后 ScrollView
【发布时间】:2016-05-09 23:11:14
【问题描述】:

我正在使用 Xamarin Forms 的最新稳定版本在 PCL 中工作。目前,目标平台是 Android。

首先,我从我的 HTTP POST 请求中获得结果,该请求运行良好。

然后,当我尝试更新 UI 时,当我向下滚动时,显示结果的滚动视图会滞后。

我认为这可能是线程问题:UI 的更新可能不会在正确的线程中执行。

我尝试使用Device.BeginInvokeOnMainThread(() => { //Update the UI });,但它不起作用。

谢谢。

【问题讨论】:

  • 在更新和滚动时 是否滞后?还是在项目更新之后 滞后?更新前滚动不会滞后吗?
  • 我保留了一个空的 stacklayout 来显示结果。我按下一个按钮,项目正确显示,然后当我尝试滚动时,它滞后了。所以之后。
  • 项目的数量可以根据请求的结果而改变,但在我的例子中,我有 9 个项目要显示。

标签: android user-interface http-post scrollview xamarin.forms


【解决方案1】:

这是我的一些代码来帮助我们:

主要方法:

private async void Search() {   

   ResponseService results = await libSearch.getResults(this.searchEntry.Text); 

   if (results.error != true) {
       List<Choice> list = (List<Choice>)results.obj;

      if(list.Count != 0) { 

         foreach(Choice choice in list) {                                    

            Device.BeginInvokeOnMainThread(() => {
               addNewChoice(choice);                                   
            });

          }  

       }
   }

AddNewChoice 方法:

private void addNewChoiceOnUI(Choice choice) {            

   ChoiceTemplate Choice = new ChoiceTemplate(choice.name, choice.img, choice.address);                           

   this.Choices.Children.Add(Choice);            
}

Web 服务调用方法 似乎有效。它看起来像这样:

 public async Task<ResponseService> getResults(string zipcode) {

 (...)

       JObject results = await JsonWebRequest.getDataFromService(queryString).ConfigureAwait(false);

 (...)

如您所见,我仅在 Web 服务调用方法和主方法上使用了 await。我还在网络服务调用方法上使用ConfigureAwait(false);。我已经尝试了许多解决方案。如果我们能找到避免延迟的解决方案,我将不胜感激。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-10
    • 2014-08-21
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多