This is a migrated thread and some comments may be shown as answers.

ListView automatic LoadOnDemand with behavior in ViewModel context

3 Answers 40 Views
This is a migrated thread and some comments may be shown as answers.
Pierre
Top achievements
Rank 1
Pierre asked on 04 Jan 2018, 07:13 PM

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?!

3 Answers, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 09 Jan 2018, 09:05 AM
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
0
Pierre
Top achievements
Rank 1
answered on 15 Jan 2018, 09:16 PM

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?

 

0
Yana
Telerik team
answered on 18 Jan 2018, 02:44 PM
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
Asked by
Pierre
Top achievements
Rank 1
Answers by
Yana
Telerik team
Pierre
Top achievements
Rank 1
Share this question
or