xamarin.forms 使用选择器更新列表视图 [英] xamarin.forms Update Listview with picker

查看:47
本文介绍了xamarin.forms 使用选择器更新列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 xamarin 表单应用程序,它的顶部有一个选择器,其下方有一个列表视图.当应用程序启动时,选择器上的数据将从 Web API 绑定.然后根据选择器值,将完成另一个 API 调用并且列表视图将加载.当用户更改选择器的值时,应该更新 Listview.我没有使用任何视图模型.我有一个用于 API 调用的帮助程序类,我将 json 结果设置为一个列表并将其设置为我的列表视图的 itemsource.我以某种方式实现了它,但它并不顺利,当我更改 picker.NB 的值时,有时会出现内存不足异常"的错误:我正在使用带有 Rg.popup.Plugin 的自定义选择器.这会将选定的值设置为标签.

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.

我的问题是

  1. 这是加载列表视图的正确方法,列表视图会在更改选择器的值时更新吗?我应该做哪些更正?
  2. 我应该为这个场景使用视图模型(MVVM)模式吗?我的列表将包含更大的数据.

我的 xamal.cs 注意: APICall 类将以正确的格式返回 json.

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.

推荐答案

第一

对于错误: 有时会得到Error as "Outof memory exception",是否需要一次性获取所有数据?如果没有,您可以只获取所需的数据并动态更新页面.例如,您可以使用分页来请求和显示数据.

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.

此外,如果您确定自己有足够的可用内存、运行 64 位操作系统并且仍然出现异常,请转到 Project properties -> Build 选项卡并确保将 x64 设置为 Platform target.just 如下:

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:

第二

由于您的应用将包含更大的数据,我们强烈建议您使用 MVVM 模式.
因为 MVVM 模式有助于将应用程序的业务和表示逻辑与其 UI 干净地分离.它还可以极大地提高代码重用机会,让我们和 UI 设计人员在开发应用的各个部分时更轻松地进行协作.

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.

这篇关于xamarin.forms 使用选择器更新列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆