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

What is the event for a swith in ListView

$
0
0

Hello,

I work with XamarinForms with a NavigationService. I work about an app about amusement park.
This view display all places available to do sort by Categories
I have this listView.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}" />-->
  <RelativeLayout>
    <SearchBar VerticalOptions="Center" HorizontalOptions="Center">
    </SearchBar>
    <ListView VerticalOptions="Center" HorizontalOptions="Center"
              x:Name="listView"
              ItemsSource="{Binding Listes}"/>
  </RelativeLayout>
</ContentPage>

I have this listView.xaml.cs :
`public partial class ListPage
{
ListPageViewModel viewModel;
    public ListPage()
    {
        InitializeComponent();
        viewModel = App.Locator.ListPageView;
        BindingContext = viewModel;
        //Init();
        InitializeTemplate();
    }

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


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

        Content = listView;

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

    /**public void Init()
    {
        this.listView.ItemsSource = (await ApiClient.FindAll<Item>()).Result;
    }*/
}`

I have this ViewModel :
`public class ListPageViewModel : ViewModelBase
{
#region Fields

    readonly INavigationService _navigationService;

    #endregion

    #region Constuctor
    public ListPageViewModel(INavigationService navigationService)
    {
        if (navigationService == null) throw new ArgumentNullException("navigationService");
        _navigationService = navigationService;

        HomeCommand = new RelayCommand(() => { _navigationService.GoBack(); });

        Parameter = new Attraction()
        {
            Attente = 20,
            CapaciteWagon = 4,
            Description = "Attraction super cool",
            Duree = 10,
            EstDejaDansLeParcours = false,
            Id = 1,
            IdType = new Type() { Id = 1, Libelle = "Type 1" },
            Latittude = 38.99,
            Longitude = 37.87,
            Libelle = "Grand splash !",
            LienGif = "http://aaa.com/a.gif",
            Ordre = 0
        };

        ItemDetailsCommand =
            new RelayCommand(() =>
            {
                _navigationService.NavigateTo(Locator.AttractionDetailsPage, Parameter);
            });

        ItineraireCommand = new RelayCommand(() =>
        {
            _navigationService.NavigateTo(Locator.AttractionDetailsPage, Parameter);
        });

        Attractions = new List<Attraction>();
        Listes = new List<Categorie>();
        InitAttractions();
        Listes = ConvertFrom(Attractions);
    }
    #endregion

    #region Properties
    public List<Attraction> Attractions { get; set; }

    #region Liste
    public List<Categorie> Listes { get; set; }
    public ICommand ItemDetailsCommand { get; set; }
    public Attraction Parameter { get; set; }
    #endregion

    #region Home
    public ICommand HomeCommand { get; set; }
    #endregion

    #region Itineraire
    public ICommand ItineraireCommand { get; set; }
    #endregion

    #endregion

    #region Methods
    List<Categorie> ConvertFrom(List<Attraction> attractions)
    {
        //TODO : Call the API and extract the full list
        //var extractSubList = Attractions.GroupBy(i => i.Type.Id);

        return new List<Categorie>() {
        new Categorie("Attractions")
        {
            new Attraction()
            {
                Attente = 20,
                CapaciteWagon = 4,
                Description = "Attraction super cool",
                Duree = 10,
                EstDejaDansLeParcours = false,
                Id = 1,
                IdType = new Type(){Id = 1, Libelle = "Type 1"},
                Latittude = 38.99,
                Longitude = 37.87,
                Libelle = "Grand splash !",
                LienGif = "http://aaa.com/a.gif",
                Ordre = 0
            },
            new Attraction()
            {
                Attente = 20,
                CapaciteWagon = 4,
                Description = "Attraction super cool",
                Duree = 10,
                EstDejaDansLeParcours = false,
                Id = 1,
                IdType = new Type(){Id = 1, Libelle = "Type 1"},
                Latittude = 38.99,
                Longitude = 37.87,
                Libelle = "Bateau pirate",
                LienGif = "http://aaa.com/a.gif",
                Ordre = 0
            },
        },
        new Categorie("Shopping")
        {
            new Attraction()
            {
                Attente = 30,
                CapaciteWagon = 8,
                Description = "Attraction pas tres cool",
                Duree = 160,
                EstDejaDansLeParcours = false,
                Id = 2,
                IdType = new Type(){Id = 2, Libelle = "Type 2"},
                Latittude = 35.20,
                Longitude = 40.60,
                Libelle = "Les galeries de césar",
                LienGif = "http://aaa.com/b.gif",
                Ordre = 0
            },
            new Attraction()
            {
                Attente = 30,
                CapaciteWagon = 8,
                Description = "Attraction pas tres cool",
                Duree = 160,
                EstDejaDansLeParcours = false,
                Id = 2,
                IdType = new Type(){Id = 2, Libelle = "Type 2"},
                Latittude = 35.20,
                Longitude = 40.60,
                Libelle = "Au poisson frais",
                LienGif = "http://aaa.com/b.gif",
                Ordre = 0
            },
        },
        new Categorie("Restaurants")
        {
            new Attraction()
            {
                Attente = 0,
                CapaciteWagon = 1,
                Description = "Attraction hyper cool",
                Duree = 20,
                EstDejaDansLeParcours = false,
                Id = 3,
                IdType = new Type(){Id = 3, Libelle = "Type 3"},
                Latittude = 37.20,
                Longitude = 10.60,
                Libelle = "Abribus",
                LienGif = "http://aaa.com/c.gif",
                Ordre = 0
            },
            new Attraction()
            {
                Attente = 0,
                CapaciteWagon = 1,
                Description = "Attraction hyper cool",
                Duree = 20,
                EstDejaDansLeParcours = false,
                Id = 3,
                IdType = new Type(){Id = 3, Libelle = "Type 3"},
                Latittude = 37.20,
                Longitude = 10.60,
                Libelle = "Boulangerie",
                LienGif = "http://aaa.com/c.gif",
                Ordre = 0
            }
        },
        new Categorie("Services")
        {
            new Attraction()
            {
                Attente = 0,
                CapaciteWagon = 1,
                Description = "Attraction hyper cool",
                Duree = 20,
                EstDejaDansLeParcours = false,
                Id = 3,
                IdType = new Type(){Id = 3, Libelle = "Type 3"},
                Latittude = 37.20,
                Longitude = 10.60,
                Libelle = "Infirmerie",
                LienGif = "http://aaa.com/c.gif",
                Ordre = 0
            },
            new Attraction()
            {
                Attente = 0,
                CapaciteWagon = 1,
                Description = "Attraction hyper cool",
                Duree = 20,
                EstDejaDansLeParcours = false,
                Id = 3,
                IdType = new Type(){Id = 3, Libelle = "Type 3"},
                Latittude = 37.20,
                Longitude = 10.60,
                Libelle = "Reception",
                LienGif = "http://aaa.com/c.gif",
                Ordre = 0
            }
        },
        new Categorie("Hotels")
        {
            new Attraction()
            {
                Attente = 0,
                CapaciteWagon = 1,
                Description = "Attraction hyper cool",
                Duree = 20,
                EstDejaDansLeParcours = false,
                Id = 3,
                IdType = new Type(){Id = 3, Libelle = "Type 3"},
                Latittude = 37.20,
                Longitude = 10.60,
                Libelle = "Le village",
                LienGif = "http://aaa.com/c.gif",
                Ordre = 0
            },
            new Attraction()
            {
                Attente = 0,
                CapaciteWagon = 1,
                Description = "Attraction hyper cool",
                Duree = 20,
                EstDejaDansLeParcours = false,
                Id = 3,
                IdType = new Type(){Id = 3, Libelle = "Type 3"},
                Latittude = 37.20,
                Longitude = 10.60,
                Libelle = "Le camp",
                LienGif = "http://aaa.com/c.gif",
                Ordre = 0
            }
        }
    };
    }
    #endregion
}`

I have this template for the Cell :
`class AttractionsListViewCell : ViewCell
{
public AttractionsListViewCell()
{
var libelleText = new Label();
libelleText.SetBinding(Label.TextProperty, "Libelle");

        var switcher = new Switch { IsToggled = false };

        switcher.SetBinding(Switch.IsToggledProperty, "EstDejaDansLeParcours");
        var view = new StackLayout()
        {
            Orientation = StackOrientation.Horizontal,
            Children =
            {
                libelleText,
                switcher
            }
        };

        View = view;
    }
}`

This object is one category :
` public class Categorie : ObservableCollection
{
public Categorie(string name)
{
Name = name;
}

    //NED TO BE PUBLIC FOR THE VIEW
    public string Name { get; set; }
}`

This class is my object from the API :

public class Attraction
    {
        public Type Type { get; set; }
        public int Id { get; set; }
        public string Libelle { get; set; }
        public string Description { get; set; }
        public object LienGif { get; set; }
        public double Latittude { get; set; }
        public double Longitude { get; set; }
        public int Attente { get; set; }
        public int CapaciteWagon { get; set; }
        public object IdType { get; set; }
        public int Duree { get; set; }
        public bool EstDejaDansLeParcours { get; set; }
        public int Ordre { get; set; }
    }

I want to get the vent when the user toogle the switch.

Could you help me to obtain it ?

The goal is to call a method to validate choices and alert user or not.

Regards


Viewing all articles
Browse latest Browse all 77050

Trending Articles



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