xamarin.forms Update Listview with picker

南笙酒味 提交于 2021-01-29 11:25:08

问题


I have xamarin forms app which have a picker at the top and a listview just beneath it.When the app launches the data on picker will bind from a web API.Then after according to the picker value another API call will be done and the listview will load. When the user changes the value of picker, then Listview should be updated.I am not using any viewmodel.I have a helper class for API call and I am setting the json result to a list and Set it as itemsource for my listview. Somehow I achieved it but it is not smooth and sometimes gets Error as "Outof memmory exception" when I change the value of picker.NB: I am using a custom picker with Rg.popup.Plugin. Which will set the selected value to a label.

My questions are

  1. Is this the proper way to load a listview which will update when changing the value of picker? What corrections should I do?
  2. Should I use a viewmodel(MVVM) pattern for this scenerio? My list will contains much larger data.

My xamal.cs NB: The APICall class will return the json in proper format.

  public partial class List : ContentPage
    {
        string weekstart;
        string WeekString;
        ObservableCollection<PickerData> resultObjcallForPicker = new ObservableCollection<PickerData>();       
        public TimeSheetList()
        {
            InitializeComponent();
            Thread loadScreenItemsThread = new Thread(new ThreadStart(LoadScreenItemsAsync));
            loadScreenItemsThread.Start();               
        }
         public async void LoadScreenItemsAsync()
         {                             
          //Picker Data loading       
            string postdataForPicker = "{\"Username\":\"" + Settings.userID + "\",\"ConnectionString\":\"" + Settings.String+ "\"}";
            APICall callForPicker = new APICall("/API/ListMobile/PickerData", postdataForPicker, loadingIndicator);
            try
            {
                    resultObjcallForPicker = callForPicker.APICallResult<ObservableCollection<PickerData>>();
                if (resultObjcallForPicker != null)
                {
                    WeekString = DateTime.Parse(resultObjcallForPicker[0].SDate).ToString("dd-MMM-yyyy");
                    Device.BeginInvokeOnMainThread(async () =>
                    {
                        // Setting the value of picker initially.
                    WeekStart.Text = WeekString;                                        
                    });
                    await loadList();                   
                }
                else
                {
                    Device.BeginInvokeOnMainThread(async () =>
                    {
                        UserDialogs.Instance.HideLoading();
                        await DisplayAlert("", "error occured", "OK");
                    });
                }
            }
            catch (Exception)
            {
                Device.BeginInvokeOnMainThread(async () =>
                {
                    UserDialogs.Instance.HideLoading();
                    ErrorMessageData errorMessage = new ErrorMessageData();
                    errorMessage.Flag = false; errorMessage.Message = callForPicker.errorMessage.Message;
                });
            }

        }

        //<<----------------Loading Listview----------------------->>   

        public async Task loadList()
        {       
            string postdataForList = "{\"date\":\"" + WeekStart.Text + "\"}";
            APICall callForList = new APICall("/API/ListMobile/ListForApproval", postdataForList, loadingIndicator);
            try
            {
               List<ListData> resultObjForListst = callForList.APICallResult<List<ListData>>();
                if (resultObjForListst != null)
                {                                                      
                    List.ItemsSource = resultObjForListst;
                    screenStackLayout.VerticalOptions = LayoutOptions.FillAndExpand;
                    List.IsVisible = true;              
               }
               else
                {
                    Device.BeginInvokeOnMainThread(async () =>
                    { 
                       await DisplayAlert("", "Please check network connection", "OK");
                    });
                }
            }
           catch (Exception)
            {
              Device.BeginInvokeOnMainThread(async () =>
               {
                   ErrorMessageData errorMessage = new ErrorMessageData();
                   errorMessage.Flag = false; errorMessage.Message = callForList.errorMessage.Message;
                });
            }

        }
        void Picker_tapped(object sender,EventArgs e)
            {
            PopupNavigation.PushAsync(new WeekStartPopUp(WeekStartList));
            MessagingCenter.Subscribe<MyMessage>(this, "WeekStartData", (value) =>
            {
                string receivedData = value.Myvalue;
                WeekStart.Text = receivedData;      

                  Device.BeginInvokeOnMainThread(async () =>
                {
                    try {
                        loadList();
                    }
                    catch(Exception Ex)
                    {

                    }

                });                       
            });
            }

}

Any help is appreciated.Please tell me if any additional information is needed.


回答1:


First

For the error: sometimes gets Error as "Outof memmory exception", do you need to get all the data at once? If not ,you can just fetch only the required data and update the page dynamically.For example, you can use paging to request and display data.

Besides, if you're sure you have enough free memory, running 64 bit OS and still getting exceptions, go to Project properties -> Build tab and be sure to set x64 as a Platform target.just as follows:

Second

Since your app will contain much larger data, we highly recommend you use MVVM pattern.
Because the MVVM pattern helps to cleanly separate the business and presentation logic of an application from its UI. It can also greatly improve code re-use opportunities and allows us and UI designers to more easily collaborate when developing our respective parts of the app.



来源:https://stackoverflow.com/questions/56718168/xamarin-forms-update-listview-with-picker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!