简体   繁体   English

指定的演员表无效 - Xamarin Forms

[英]Specified cast is not valid - Xamarin Forms

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));所以我试图导航到一个内容页面,该页面应该包含用户在收藏视图中选择的电影中的所有电影详细信息,但每次我 select 在我的收藏视图中观看一部电影时,都会出现错误“指定演员表无效”,这发生在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.我对 C# 和使用 Xamarin forms 非常陌生,所以我不确定这是否是正确的做事方式。

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;
            
        }
    }
}

As Jason said,正如杰森所说,

CurrentSelection – the list of items that are selected, after the selection change. CurrentSelection – 选择更改后选择的项目列表。

So you could set SelectionMode to Single ,then use SelectedItem .所以你可以将SelectionMode设置为Single ,然后使用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 .您可以查看的越多CollectionView Selection

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 指定的强制转换无效(xamarin 形式) - Specified cast is not valid (xamarin forms) 指定的转换在 Xamarin Forms 中无效 - Specified cast is not valid in Xamarin Forms Xamarin.Forms 绑定指定的强制转换无效 - Xamarin.Forms Binding Specified cast is not valid Xamarin.Forms:使用 Picker 时错误指定的强制转换无效 - Xamarin.Forms: Error Specified cast is not valid when using Picker Xamarin 形式:System.InvalidCastException:“指定的强制转换无效。” - Xamarin forms: System.InvalidCastException: 'Specified cast is not valid.' Xamarin forms: System.InvalidCastException: '指定的转换无效 - Xamarin forms: System.InvalidCastException: 'Specified cast is not valid Xamarin 表单与 Firebase。 数据检索抛出 System.InvalidCastException: &#39;指定的转换无效。&#39; - Xamarin Forms with Firebase. Data retrival throwing System.InvalidCastException: 'Specified cast is not valid.' 为什么从 sqlite db 中选择会显示错误“指定的强制转换无效”。 在 Xamarin 表单中? - Why does select from sqlite db shows error 'Specified cast is not valid.' in Xamarin Forms? 在 Xamarin Forms 中获取 SelectedItem 值时,选择器错误“指定的转换无效” - Picker error “Specified cast is not valid” when getting the SelectedItem value in Xamarin Forms Xamarin Forms:在可绑定的内部添加全局值时指定的转换无效 object:列表视图 - Xamarin Forms: Specified cast is not valid while adding a global value inside a bindable object: list view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM