Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

How to thread itemsSource loading of a listView

$
0
0

Hello,

I have an app (Xamarin.Forms portable) with MVVMLight with a navigation service.
The BindingContext have a list of object which are some observableCollection.

I launch the app, go to the ListPage, listView is correctly set with my customCell template.
I change one object with the slider, to make a boolean to true.
I go back the the homepage, I try to go to the ListPage, error with this message : Skipped xxxx frames! The application may be doing too much work on its main thread.

I tried to cal the thread on the device, to create a task and nothing is working.
I have datas as test, but I will call an API client because I develop a webservice too.

My xaml.cs
` public partial class ListPage
{
ListPageViewModel viewModel;

    public ListPage()
    {
        InitializeComponent();
        viewModel = App.Locator.ListPageView;
        viewModel.IsBusy = true;
        viewModel.InitListAttractions(); //<= load datas for testing 
        BindingContext = viewModel;
        InitializeTemplate();
    }

    void InitializeTemplate()
    {
        var dataTemplate = new DataTemplate(typeof(CustomSwitchCell));
        dataTemplate.SetBinding(TextCell.TextProperty, "Libelle");
        dataTemplate.SetBinding(Switch.IsToggledProperty, "EstDejaDansLeParcours");
        listView.ItemTemplate = dataTemplate;

        var listViewLocal = new ListView()
        {
            IsGroupingEnabled = true,
            GroupDisplayBinding = new Binding("Name"),
            GroupShortNameBinding = new Binding("Name"),
            ItemsSource = viewModel.Listes,
            ItemTemplate = dataTemplate
        };

        listViewLocal.ItemSelected += (sender, e) =>
        {
            var attraction = (Attraction)listViewLocal.SelectedItem;
            viewModel.Parameter = attraction;
            viewModel.ItemDetailsCommand.Execute(viewModel.Parameter);
        };

        Content = listViewLocal;
        viewModel.IsBusy = false;
    }
}`

My custom cell :
`public class CustomSwitchCell : ViewCell
{
public event EventHandler SwitchTapped;

    public CustomSwitchCell()
    {
        var stack = new StackLayout()
        {
            Orientation = StackOrientation.Horizontal,
            Padding = new Thickness(10, 5, 10, 5),
            Spacing = 0
        };

        var nameLabel = new Label
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            FontSize = Device.GetNamedSize(NamedSize.Default, typeof(Label)),
            LineBreakMode = LineBreakMode.NoWrap,
            TextColor = Color.White
        };
        nameLabel.SetBinding(Label.TextProperty, "Libelle");
        stack.Children.Add(nameLabel);

        var toggleSwitch = new Switch { HorizontalOptions = LayoutOptions.End };
        toggleSwitch.SetBinding(Switch.IsToggledProperty, "EstDejaDansLeParcours");
        stack.Children.Add(toggleSwitch);

        View = stack;
    }
}`

My xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ParkHelper.Views.ListPage"
             Title="Attractions">
  <!--  <Button Text="Go to details" Command="{Binding ItemDetailsCommand}" />-->
  <Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
      <SearchBar VerticalOptions="Center" HorizontalOptions="Center">
      </SearchBar>
      <ListView VerticalOptions="Center" HorizontalOptions="Center"
                x:Name="listView"
                ItemsSource="{Binding Listes}"/>
    <ActivityIndicator
                      IsVisible="{Binding IsBusy}"
                      IsRunning="{Binding IsBusy}"
                      Color="Black" />
  </Grid>
</ContentPage>

Could you help me to thread it ?

Regards


Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>