[英]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.