0
votes

So I am trying to navigate to a content page that is supposed to have all the movie details from the movie the user selects in the collection view but every time I select a movie in my collection view the error "Specified cast is not valid", which occurs at the Navigation.PushAsync(new MovieDetails((MovieData)e.CurrentSelection)); line, pops up. I'm very new to C# and using Xamarin forms so I'm not sure if this is the right way to be doing things.

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using Listly.Models;
using Xamarin.Forms;

namespace Listly
{
    public partial class SearchPage : ContentPage
    {
        public ObservableCollection<MovieData> allMovies = new ObservableCollection<MovieData>();
        public int NumClicked { get; set; }
        public bool BtnClicked = false;
        public string Poster_Path_End { get; set; }
        public MovieList newList = new MovieList();

        public SearchPage()
        {
            InitializeComponent();

            contentPage.BackgroundColor = Color.FromHex("1D1D1D");
            searchBtn.BackgroundColor = Color.FromHex("28D7B5");
            searchBtn.TextColor = Color.FromHex("1D1D1D");
            searchBar.PlaceholderColor = Color.FromHex("A0A0A0");
            searchBar.TextColor = Color.Black;

           
            collectionView.SelectionMode = SelectionMode.Single;

        
            collectionView.ItemsSource = allMovies;

            collectionView.SelectionChanged += CollectionView_SelectionChanged;

            searchBtn.Clicked += SearchBtn_Clicked;

        }

        private void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.CurrentSelection != null)
            {
                 Navigation.PushAsync(new MovieDetails((MovieData)e.CurrentSelection));

            }

        }

        private async void SearchBtn_Clicked(object sender, EventArgs e)
        {
          
            if (BtnClicked == false)
            {
                BtnClicked = true;
                NumClicked += 1;

                DataManager dataManager = new DataManager(searchBar.Text);
           dataManager.GetMovie(searchBar.Text);
                newList = await dataManager.GetMovie(searchBar.Text);
                foreach (var movie in newList.searchList)
                {
                    if (movie.Title == null)
                    {
                        bool answer = await DisplayAlert("Movie Not Found", "The movie searched was not found. Please try again.", "Okay", "Cancel");
                        searchBar.Text = "";
                        searchBar.Placeholder = "Enter Movie Name";
                        break;
                    }
                    allMovies.Add(movie);
                }
            }
            else if (BtnClicked == true)
            {
                if (NumClicked == 1 || NumClicked > 1)
                {
                    allMovies.Clear();

                  
                    DataManager dataManager = new DataManager(searchBar.Text);
                  
                    newList = await dataManager.GetMovie(searchBar.Text);
                    foreach (var movie in newList.searchList)
                    {
                        if (movie.Title == null)
                        {
                            bool answer = await DisplayAlert("Movie Not Found", "The movie searched was not found. Please try again.", "Okay", "Cancel");
                            break;
                        }
                    
                       allMovies.Add(movie);
                    }

                }
            }

            collectionView.ItemsSource = allMovies;
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

          
            collectionView.ItemsSource = allMovies;

        }
    }
}

Here's the MovieDetails page

using System;
using System.Collections.Generic;
using Listly.Models;
using Xamarin.Forms;

namespace Listly
{
    public partial class MovieDetails : ContentPage
    {
        public MovieData details = new MovieData();
        public MovieData selectedMovie = new MovieData();
        public string Poster_Path_End { get; set; }


        public MovieDetails(MovieData selectedMovie)
        {
            InitializeComponent();
          
            details = selectedMovie;
            details.Title = selectedMovie.Title;
            details.Overview = selectedMovie.Overview;
            
        }
    }
}
1
a CollectionView can have multiple items selected, so e.CurrentSelection is a collection of items. You can use the SelectedItem property of the CollectionView if you have SelectionMode set to Single - Jason
you can add a breakpoint to the line where you get the error and check the type of e.CurrentSelection. I think it is not MovieData - 久违的恐惧感

1 Answers

0
votes

As Jason said,

CurrentSelection – the list of items that are selected, after the selection change.

So you could set SelectionMode to Single,then use SelectedItem.

or do like this:

MovieData movie = e.CurrentSelection.FirstOrDefault() as MovieData ;
Navigation.PushAsync(new MovieDetails(movie);

the more you could look at CollectionView Selection.