在xamarin.forms中单击按钮以调用异步任务

时间:2019-06-27 05:11:51

标签: xamarin.forms

我有xamarin.forms应用程序包含一个可从Rest API加载值的列表视图,工作正常,我在列表视图上方有一个按钮,当我单击该按钮时,将再次放置该列表视图API调用,并且listview应该更新。但是停留在这个更新部分。我没有使用MVVM模式。listview列表部分是一个异步任务。单击按钮时我再次调用异步任务,但是App崩溃了。是由于单击按钮再次调用异步任务吗?任何帮助表示赞赏。

这是我的代码。

 namespace app
    {
        public partial class List : ContentPage
        {   
            PendingWeekRange pendingWeekRange = new PendingWeekRange();
            public TimeSheetList()
            {
                InitializeComponent();        
                Task.Run(async () =>
                {
                    await LoadScreenItems();
                });           
            }    
            async Task LoadScreenItems()
            {
               await Task.Run(async () => {               
                    try
                    {                 
                          // Doing some stuff

                            await loadTimeSheetList();               
                    }
                    catch (Exception)
                    {

                    }
                });
            }  
            async Task loadTimeSheetList()
            {
                await Task.Run(() => {  +  string postdataForPendingList = "{\"date\":\"" + "1" + "\"}";
                APICall callForAPICallResult = new APICall("/API/ListMobile/ListForApproval", postdataForList, loadingIndicator);       
                    try
                    {                                        
                        List<ListData> resultObjForPendingTimeSheetList = callForAPICallResult<List<ListData>>();
                        if (resultObjForPendingTimeSheetList != null)
                        {

                            TimesheetList.ItemsSource = resultObjForPendingTimeSheetList;
                            screenStackLayout.VerticalOptions = LayoutOptions.FillAndExpand;
                            TimesheetList.IsVisible = true;
                        }
                        else
                        {

                        }
                    }
                    catch (Exception)
                    {                    
                    }
                });          
            }
         async   void Button_Tapped(object sender, EventArgs e)
            {
                try
                {            
                      // Calling my listview again. After calling app gets crash                
                  Task.Run(async () => await loadTimeSheetList());                           
                }
                catch (Exception ex) { }
            } 
        }
    }

2 个答案:

答案 0 :(得分:1)

解决问题之前的一些事情。您有异步/等待所有错误,请通过Async Programming

Task.Run在另一个线程上运行传递的操作,如果您对此线程上的UI元素进行更改,则您的应用肯定会崩溃(相信我的话)。

如果您要在页面启动时进行异步调用,请使用OnAppearing方法(如果您只想调用一次,请保留一个标志)

请勿频繁更改列表视图的ItemsSource,只需清除并添加项目即可。

namespace app
{
    public partial class List : ContentPage
    {   
        PendingWeekRange pendingWeekRange = new PendingWeekRange();

        private ObservableCollection<ListData> TimesheetObservableCollection = new ObservableCollection<ListData>();
        public TimeSheetList()
        {
            InitializeComponent();          
            TimesheetList.ItemsSource = TimesheetObservableCollection;
        }
        protected override async OnAppearing()
        {
            // flag for first launch?
            await LoadScreenItems();

        }
        async Task LoadScreenItems()
        {     
            try
            {                 
                    // Doing some stuff
                    TimesheetObservableCollection.Clear();
                    TimesheetObservableCollection.AddRange(await GetTimeSheetList());
            }
            catch (Exception)
            {
                //handle exception
            }
        }  
        async Task<List<ListData>> GetTimeSheetList()
        {
            string postdataForPendingList = "{\"date\":\"" + "1" + "\"}";
            APICall callForAPICallResult = new APICall("/API/ListMobile/ListForApproval", postdataForList, loadingIndicator);       
            try
            {                                        
                return callForAPICallResult<List<ListData>>();
            }
            catch (Exception) 
            { 
                // handle exception
            }        
        }
        async void Button_Tapped(object sender, EventArgs e)
        {
            try
            {
                // Calling my listview again. After calling app gets crash                
                TimesheetObservableCollection.Clear();
                TimesheetObservableCollection.AddRange(await GetTimeSheetList());
            }
            catch (Exception ex) { }
        } 
    }
}

答案 1 :(得分:0)

@Androdevil,

以此更新您的loadTimeSheetList,

async Task loadTimeSheetList()
    {

            try
            {            
                // I am calling my API for Listview here.            
                List<TimeSheetListData> resultObjForPendingTimeSheetList = await callForPendingTimeSheetList.APICallResult<List<TimeSheetListData>>();
                if (resultObjForPendingTimeSheetList != null)
                {

                    TimesheetList.ItemsSource = resultObjForPendingTimeSheetList;
                    screenStackLayout.VerticalOptions = LayoutOptions.FillAndExpand;
                    TimesheetList.IsVisible = true;
                }
                else
                {

                }
            }
            catch (Exception)
            {                    
            }

    }