ListView automatic LoadOnDemand with behavior in ViewModel context

4 posts, 0 answers
  1. Pierre
    Pierre avatar
    231 posts
    Member since:
    Apr 2007

    Posted 04 Jan Link to this post

    Hi!

    I follow your sample for the Xamarin.Forms listview. But I need to use bahavior to respect the MVVM patturn. So I go my 2 behavior (one for selection and the other for LoadOnDemand)

    lvAppels.Behaviors.Add(new EventToCommandBehavior
    {
        EventName = "ItemTapped",
        Command = ((AppelsViewModel)BindingContext).ItemTappedCommand,
        Converter = new ItemTappedEventArgsConverter()
    });
    lvAppels.Behaviors.Add(new EventToCommandBehavior
    {
        EventName = "LoadOnDemand",
        Command = ((AppelsViewModel)BindingContext).LoadOnDemandCommand
    });

    That work good but when the Listview is created or when I scroll down the LoadOnDemand is called several time. I think is because I do not call 

    this.listView.IsLoadOnDemandActive = false;

    But how I can do that from the viewModel without breaking the MVVM?!

  2. Yana
    Admin
    Yana avatar
    4630 posts

    Posted 09 Jan Link to this post

    Hello Pierre,

    Since you're using the MVVM pattern, I'd recommend you load the items through the ListViewLoadOnDemandCollection as it takes care to request the items internally, so you can do the work in the view model .ListViewLoadOnDemandCollection can work with in Automatic mode without a problem. This section in the documentation shows how to use the collection. 

    Please give it a try and let me know if you have any additional questions or concerns.

    Regards,
    Yana
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Pierre
    Pierre avatar
    231 posts
    Member since:
    Apr 2007

    Posted 15 Jan in reply to Yana Link to this post

    I don't understand how to handle my collection correctly with your code.

    I got a RadSegmentedControl that will change the filter of my Listview Source. I was using ObservableRangeCollection with RaisePropertyChanged to bind to the ListView. In case of filter change with the SegmentedControl, I just do a collection.ReplaceRange(...) and the RaisePropertyChnaged noyify the Listview to refresh. And when the "Load more item" was called, I use the AddRange function. 

    How I can achieve the same thing with.

    public ListViewLoadOnDemandCollection Appels { get; set; } without RaisePropertyChnaged  when I need to delete the current collection and start over with a new filter?

     

  4. Yana
    Admin
    Yana avatar
    4630 posts

    Posted 4 days and 1 hour ago Link to this post

    Hi Pierre,

    Thank you for sending the additional details on the scenario you have.

    ListViewLoadOnDemandCollection inherits from ObservableCollection, so one option is to extend it with the methods for implementing RangeCollection functionality (AddRange, ReplaceRange).

    However, I came upon a simpler solution - you could still use the LoadOnDemand event and bind the IsLoadOnDemandActive property of the ListView to a boolean property in your ViewModel - in this way you will be able to switch it without having the ListView instance.

    Let me know whether such a solution would be suitable for you.

    Regards,
    Yana
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top