Search code examples
mvvmdynamicxamarin.formsprismtabbedpage

Creating Dynamic TabbedPage using Prism Xamarin forms


I am using Prism with item source, Inside each tabbed page there is a Listview for which i am unable utilize "ItemTapped" event as an event to command Behaviour to trigger a command in viewmodel when an listview item is tapped, but there is no trigger in debug mode when i tap an list view item, Please help me understand why this is happening and is there any alternate way in which i can use command for this. Also when i checked The event is getting triggered in PrismTabbedPage1.xaml.cs but not in PrismTabbedPage1ViewModel.cs

Prism Xamarin forms

PrismTabbedPage1.Xaml

<?xml version="1.0" encoding="utf-8" ?>
 <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms" xmlns:behaviors="clr-namespace:Prism.Behaviors;assembly=Prism.Forms" prism:ViewModelLocator.AutowireViewModel="True" x:Class="testapp.Views.PrismTabbedPage1" ItemsSource="{Binding countries}"> <TabbedPage.ItemTemplate> 
<DataTemplate> 
<ContentPage Title="{Binding CountryName}">
 <ListView ItemsSource="{Binding Cities}"> 
<ListView.ItemTemplate>
 <DataTemplate>
 <ViewCell> 
<Label Text="{Binding CityName}" TextColor="Black"/> 
</ViewCell>
 </DataTemplate>
 </ListView.ItemTemplate> 
<ListView.Behaviors> 
<behaviors:EventToCommandBehavior EventName="ItemTapped" Command="{Binding ListItemTapped}" EventArgsParameterPath="Item"/> 
</ListView.Behaviors> </ListView>
 </ContentPage>
 </DataTemplate>
 </TabbedPage.ItemTemplate>
 </TabbedPage>`

PrismTabbedPage1ViewModel.cs

using Prism.Mvvm;
using Prism.Navigation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using testapp.Models;


namespace testapp.ViewModels
{
    public class PrismTabbedPage1ViewModel : BindableBase
    {
        public ObservableCollection<Country> countries { get; set; }
        public DelegateCommand<Country> ListItemTapped => new DelegateCommand<Country>(OnTapped);

        private void OnTapped(Country obj)
        {

            //NavigationService.NavigateAsync("MainPage");
        }

        public PrismTabbedPage1ViewModel()
        {
            ObservableCollection<City> Cities1 = new ObservableCollection<City>()
            {
                new City(){ CityName = "City1" },new City(){ CityName = "City2" },new City(){ CityName = "City3" },
                new City(){ CityName = "City4" },new City(){ CityName = "City5" },new City(){ CityName = "City6" },
                new City(){ CityName = "City7" },new City(){ CityName = "City8" },new City(){ CityName = "City9" },
            };


            countries = new ObservableCollection<Country>()
            {
                new Country(){ CountryName = "Country 1", Cities = Cities1 },
                new Country(){ CountryName = "Country 2", Cities = Cities1 },
                new Country(){ CountryName = "Country 3",  Cities = Cities1 },
            };
        }
    }
}

Country.cs

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace testapp.Models
{
public class Country
{
public string CountryName { get; set; }
public ObservableCollection Cities { get; set; }
}
public class City
{
public string CityName { get; set; }


}

}

In this code the listview command behaviour is not get triggered.


Solution

  • I'm not familiar with Prism but since your EventToCommand is inside a ListView, your default binding is the data passed by the listview, and not your ViewModel.

    You could try something like this :

    <behaviors:EventToCommandBehavior EventName="ItemTapped"
                             Command="{Binding Source={x:Reference this},Path=BindingContext.ListItemTapped}"
                             EventArgsParameterPath="{Binding .}"/>
    

    And give a name to your page :

     <TabbedPage .... x:Name="this">
    

    So your binding inside your listview should effectively redirect to the command in your viewmodel.

    I hope this helps.