When I tap on a menu item, it is supposed to display a page containing several menu and sub-menu items, but it is giving the above error.
This is how I am trying to access the content page from my appshell.xaml:
<FlyoutItem Title="Services" Icon="icon_feed.png">
<ShellContent Route="Hotels" ContentTemplate="{DataTemplate local:Hotels}" />
This is the Hotels.xaml.cs file I am trying to display in the Views Folder:
namespace AbuseAlert.Views
{
public partial class Hotels : ContentPage
{
private HotelsGroupViewModel ViewModel
{
get { return (HotelsGroupViewModel)BindingContext; }
set { BindingContext = value; }
}
private List<Hotels> ListHotel = new List<Hotels>();
protected override void OnAppearing()
{
try
{
base.OnAppearing();
if (ViewModel.Items.Count == 0)
{
ViewModel.LoadHotelsCommand.Execute(null);
}
}
catch (Exception Ex)
{
Debug.WriteLine(Ex.Message);
}
}
public Hotels(HotelsGroupViewModel viewModel)
{
InitializeComponent();
this.ViewModel = viewModel;
}
}
}
This is the Hotels.xaml file:
<?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:Name="currentPage"
x:Class="AbuseAlert.Views.Hotels">
<ContentPage.Content>
<Grid >
<StackLayout x:Name="hotelStack" Padding="1,0,1,0" >
<ListView
x:Name="HotelsList"
BackgroundColor="White"
IsGroupingEnabled="True"
IsPullToRefreshEnabled="true"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
ItemsSource="{Binding Items}"
RefreshCommand="{Binding LoadHotelsCommand}"
>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" VerticalOptions="Center">
<Label
VerticalOptions="Center"
FontAttributes="Bold"
FontSize="Medium"
Text="{Binding RoomName}"
TextColor="Black"
VerticalTextAlignment="Center" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<Grid >
<Label
FontAttributes="Bold"
FontSize="Small"
Text="{Binding Name}"
TextColor="Gray"
VerticalTextAlignment="Center" />
<Image x:Name="ImgA" Source="{Binding StateIcon}" Margin="0,0,5,0" HeightRequest="20" WidthRequest="20" HorizontalOptions="End"/>
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Source={x:Reference currentPage}, Path=BindingContext.RefreshItemsCommand}" NumberOfTapsRequired="1" CommandParameter="{Binding .}"/>
</Grid.GestureRecognizers>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
</ListView>
</StackLayout>
</Grid>
</ContentPage.Content>
So, what do I do next to clear this error and get the content page to display?
This is HotelsGroupViewModel.cs.
using AbuseAlert.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using Xamarin.Forms;
namespace AbuseAlert.ViewModels
{
public class HotelsGroupViewModel : BaseViewModel2
{
private HotelViewModel _oldHotel;
private ObservableCollection<HotelViewModel> items;
public ObservableCollection<HotelViewModel> Items
{
get => items;
set => SetProperty(ref items, value);
}
public Command LoadHotelsCommand { get; set; }
public Command<HotelViewModel> RefreshItemsCommand { get; set; }
public HotelsGroupViewModel()
{
items = new ObservableCollection<HotelViewModel>();
Items = new ObservableCollection<HotelViewModel>();
LoadHotelsCommand = new Command(async () => await ExecuteLoadItemsCommandAsync());
RefreshItemsCommand = new Command<HotelViewModel>((item) => ExecuteRefreshItemsCommand(item));
}
public bool isExpanded = false;
private void ExecuteRefreshItemsCommand(HotelViewModel item)
{
if (_oldHotel == item)
{
// click twice on the same item will hide it
item.Expanded = !item.Expanded;
}
else
{
if (_oldHotel != null)
{
// hide previous selected item
_oldHotel.Expanded = false;
}
// show selected item
item.Expanded = true;
}
_oldHotel = item;
}
async System.Threading.Tasks.Task ExecuteLoadItemsCommandAsync()
{
try
{
if (IsBusy)
return;
IsBusy = true;
Items.Clear();
List<Room> Hotel1rooms = new List<Room>()
{
new Room("Record Audio", 1), new Room("Take a Photo", 1), new Room("Record Video", 1), new Room("Watch a LiveStream", 1), new Room("Broadcast a LiveStream", 1)
};
List<Room> Hotel2rooms = new List<Room>()
{
new Room("View RED Zone", 1), new Room("Manage RED Zone", 1)
};
List<Room> Hotel3rooms = new List<Room>()
{
};
List<Room> Hotel4rooms = new List<Room>()
{
};
List<Room> Hotel5rooms = new List<Room>()
{
};
List<Room> Hotel6rooms = new List<Room>()
{
new Room("Support Tips", 1), new Room("Counselling Tips", 1), new Room("Chat", 1)
};
List<Room> Hotel7rooms = new List<Room>()
{
};
List<Hotel> items = new List<Hotel>()
{
new Hotel("Report Abuse", Hotel1rooms), new Hotel("RED Zone", Hotel2rooms), new Hotel("Location Inquiry", Hotel3rooms), new Hotel("Notify", Hotel4rooms), new Hotel("Voice2Text", Hotel5rooms), new Hotel("Support & Counselling", Hotel6rooms), new Hotel("Options", Hotel7rooms)
};
if (items != null && items.Count > 0)
{
foreach (var hotel in items)
Items.Add(new HotelViewModel(hotel));
}
else { IsEmpty = true; }
}
catch (Exception ex)
{
IsBusy = false;
Debug.WriteLine(ex);
}
finally
{
IsBusy = false;
}
}
}
}