隐藏

c# – Xamarin表单ListView程序刷新在页面加载时不会在Android上停止

发布:2021/10/20 13:35:48作者:管理员 来源:本站 浏览次数:904

我在Visual Studio 2015中有一个带有ListView的Portable项目,它通过以下函数refreshData通过API调用填充了一些数据:


   async Task refreshData()

   {

       myListView.BeginRefresh();

       var apiCallResult = await App.Api.myApiGetCall();            

       myListView.ItemsSource = apiCallResult;

       myListView.EndRefresh();    

   }


调用refreshData()


   protected override void OnAppearing()

   {

       base.OnAppearing();

       refreshData();

   }


一切正常,除了在Android上,当初始加载页面时,刷新指示器没有停止或消失在EndRefresh()上.该页面位于TabbedPage中,因此我可以转到另一个选项卡,然后返回到此页面,刷新指示器正确启动和停止,并完成我的API调用.


为什么在Android最初加载页面时刷新不会停止?任何帮助,将不胜感激.


注意:当我在iOS上运行时,这完全正常.


到目前为止,我已经尝试过:


>用myListView.IsRefreshing = true替换myListView.BeginRefresh(),用myListView.IsRefreshing = false替换myListView.EndRefresh()

>使用Device.BeginInvokeOnMainThread(()=> {//更新列表和endRefresh}).

>使用async void refreshData()而不是async Task refreshData().

解决方法

试试这个:


   async void refreshData()

   {

       Device.BeginInvokeOnMainThread(() => {

           myListView.BeginRefresh();

           var apiCallResult = await App.Api.myApiGetCall();

           myListView.ItemsSource = apiCallResult;

           myListView.EndRefresh();

       });

   }


显然,不再需要“任务”了.


如果只在Android中出现错误,则可能是Android处理线程的方式.它不允许线程直接更改可视元素.有时当我们尝试这样做时会抛出一个静默异常,代码操作没有实际效果.