No able to bind values to the BindablePicker - can anybuddy help me

I am using basic MVVM structure and I am trying to bind data from API form my ViewModel using async when I use static record its loading the picker but when I try to add dynamic records I am not able to bind the record. my screen getting frozen.

If I am not able to resolve by tomorrow my manager will not allow me to work on Xamarin anymore.

My Registration.xamal

RegistrationViewModel.cs

public int HPSelectedListValue { get; set; }
public IEnumerable HPList { get; set; }

public RegistrationViewModel()
    {
        bindPiker();
    }

public async void bindPiker()
{
var url = "api url "; // this is the value return by json [{"id":1,"name":"Blue "},{"id":2,"name":"Red"}]
var client = new HttpClient();
var json = await client.GetStringAsync(url);
HPList = JsonConvert.DeserializeObject<List>(json); **// its allocating data to HPList but its not able to reflect on the picker **

** //Here is the commented code which is working fine**

            //List<DropdownModel> SEList = new List<DropdownModel>();
            //DropdownModel s = new DropdownModel();
            //s.ID = 0;
            //s.Name = "SysElement 0";
            //SEList.Add(s);

            //s = new DropdownModel();
            //s.ID = 1;
            //s.Name = "SysElement 1";
            //SEList.Add(s);
            //HPList = SEList;

}

Best Answer

Answers

  • rene_ruppertrene_ruppert DEXamarin Team, University, XamUProfessors Xamurai

    On the one hand you are writing that your app is freezing, on the other hand you say that HPList is assigned to correctly but data is not reflected in the picker. That's making it a bit hard to diagnose.

    You should consider refactoring your code a bit and don't call an async method from within the constructor.

    Add a method to your view model:

    public Task InitializeAsync()
    {
      return BindPicker();
    }
    

    Make sure to change your BindPicker method to return a Task and not void . Also, BindPicker() should not replace HPList but rather Clear() it and then Add() the items (I presume, HPList is an ObservableCollection<T>).

    In the page you're using the VM from, override OnAppearing

    public async void override OnAppearing()
    {
      await vm.InitializeAsync();
      // Here any other setup you have to do on your page.
    }
    

    This makes sure that all exceptions will be caught and shown correctly and you prevent race conditions.

  • PinakinPatel.3782PinakinPatel.3782 USUniversity

    Hey Rene

    Thanks for your response, I am trying below mentioned PickerBindBehavior.

    I am using

      var httpClient = new HttpClient();
                var url = "Data link "; // {ID= 1, Name="Person"}
                var responseString = await httpClient.GetStringAsync(url);
                FeedItems = JsonConvert.DeserializeObject<ObservableCollection<DropdownModel>>(responseString);
    

    FeedItems is Observable collection which have 4 value right now.

    When I run the project is show me picker value as per attached image

    Could you please help me to fixed it.

    https://github.com/xamarinhq/xamu-infrastructure/wiki/PickerBindBehavior

  • PinakinPatel.3782PinakinPatel.3782 USUniversity
    Accepted Answer

    Hey Rene

    I have finally resolve this. now I am able to bind my entire api result to picker

Sign In or Register to comment.