I am trying to implement MVVM into my xamarin app, but it crashes saying 'indexer did not contain closing bracket'

127 Views Asked by At

This is part of my xaml code, where the binding happens:

      <ListView  
              ItemsSource="{Binding PenStocks}"
              HasUnevenRows="True"
              ItemSelected="Event_ItemSelected"
              SeparatorVisibility="None"
              Margin="5,5,5,0">
    <ListView.ItemTemplate>

This is the code behind of it:

     public partial class MainPage : ContentPage
    {
    public MainPage()
    {
        InitializeComponent();

        BindingContext = new PenStockListViewModel(new PageService());
    }
    private async void Event_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        await (BindingContext as PenStockListViewModel).PenStockSelected(e.SelectedItem as PenStockViewModel);
    }
    }

This is the viewmodel class of the list, which is the source of the objects that are bound to the listView. (Note: the FillPenstockData and SetRotationData methods are not given to keep it short here)

      class PenStockListViewModel:PenStockBaseViewModel
{
    public ObservableCollection<PenStockViewModel> PenStocks { get; set; } = new ObservableCollection<PenStockViewModel>();

    private PenStockViewModel _selectedPenStock;
    public PenStockViewModel SelectedPenStock
    {
        get
        { return _selectedPenStock; }

        set
        {
            if (_selectedPenStock == value)
                return;

            _selectedPenStock = value;
            OnPropertyChanged();
        }
    }
    private readonly IPageService _ipageService;

    public PenStockListViewModel(IPageService pageService)
    {
        _ipageService = pageService;
        FillPenStockData();
        SetRotationData();
    }

This is the viewmodel class of a single object of PenStock:

       public class PenStockViewModel: PenStockBaseViewModel
{
    public string Name { get; set; }
    public ObservableCollection<Graph> Data { get; set; }

    private double rotations;
    public string Unit { get; set; }
    public double Rotations
    {
        get { return rotations; }

        set
        {
            rotations = value;
        }
    }
}

This is BaseViewModel of Penstock class:

        public class PenStockBaseViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

This is my IPageService interface:

      interface IPageService
{
    Task PushAsync(Page page);
}

This is my PageService class:

     class PageService : IPageService
{
    public async Task PushAsync(Page page)
    {
        await Application.Current.MainPage.Navigation.PushAsync(page);
    }
}

The app works when loaded and the list is loaded perfectly onto the mainpage, but when I select an item, it crashes and enters debugging mode saying 'indexer did not contain closing bracket'. I want it move to the next detail page of the selected item, but doesn't happen. THis is my first app I am using MVVM and I have not fully implemented it yet as I still have remove the event handlers and use ICommand interface. Please help to figure out this error.

0

There are 0 best solutions below