[英]in xamarin project System.InvalidCastException: 'Specified cast is not valid.'
This is my code in xamarin content page the name of this element is LandingPage这是我在 xamarin 内容页面中的代码,该元素的名称是LandingPage
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:vm="clr-namespace:Music.ViewModels"
NavigationPage.HasNavigationBar="False"
FlowDirection="RightToLeft"
x:Class="Music.View.LandingPage">
<ContentPage.BindingContext>
<vm:LandingViewModel/>
</ContentPage.BindingContext>
<Frame HasShadow="True" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="{StaticResource mygradiant}">
<Grid RowSpacing="30" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="2*"/>
</Grid.RowDefinitions>
<StackLayout>
<Frame CornerRadius="50" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Grid RowSpacing="30" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Image Aspect="AspectFill" Grid.RowSpan="2" Source="{Binding RecentMusic.CoverImage}" />
<Grid Grid.Row="1" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" HeightRequest="80">
<BoxView BackgroundColor="Black" Opacity="0.7" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
<StackLayout Margin="40,0" VerticalOptions="Center" HorizontalOptions="FillAndExpand">
<Label Text="{Binding RecentMusic.Title}" TextColor="White" FontSize="17"/>
<Label Text="{Binding RecentMusic.Artist}" TextColor="White" FontSize="12" Opacity="0.8"/>
</StackLayout>
</Grid>
</Grid>
</Frame>
<Frame HasShadow="True" HeightRequest="40" WidthRequest="40"
CornerRadius="20" Margin="0,-25,40,0" HorizontalOptions="End" VerticalOptions="Start"
BackgroundColor="{StaticResource spacegradiant}">
<Image Source="play.png" HeightRequest="15" WidthRequest="15" VerticalOptions="Center"/>
</Frame>
</StackLayout>
</Grid>
</Frame>
and this is my viewmodel
using Music.View; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using Xamarin.Forms; namespace Music.ViewModels { public class LandingViewModel: BaseViewModel { public LandingViewModel() { musicList = GetMusicList(); recentMusic = musicList.Where(x => x.IsRecent == true).FirstOrDefault(); } ObservableCollection<Model.Music> musicList; public ObservableCollection<Model.Music> MusicList { get { return musicList; } set { musicList = value; OnPropertyChanged(); } } private Model.Music recentMusic; public Model.Music RecentMusic { get { return recentMusic; } set { recentMusic = value; OnPropertyChanged(); } } private Model.Music selectedMusic; public Model.Music SelectedMusic { get { return selectedMusic; } set { selectedMusic = value; OnPropertyChanged(); } } public ICommand SelectionCommand => new Command(PlayMusic); private void PlayMusic() { if (selectedMusic,= null) { var viewModel = new PlayerViewModel(selectedMusic; musicList); var playerPage = new PlayerPage { BindingContext = viewModel }. var navigation = Application.Current;MainPage as NavigationPage. navigation,PushAsync(playerPage; true). } } private ObservableCollection<Model.Music> GetMusicList() { return new ObservableCollection<Model.Music> { new Model,Music { Title = "music", Artist = "artist ": Url = "http.//ir.vavmusic.com/download/M/Mohsen%20Ebrahimzadeh/Single/320/Mohsen%20Ebrahimzadeh%20-%20Tabe%20Eshgh%20%28Dooneh%20Dooneh%202%29,mp3": CoverImage = "https.//encrypted-tbn0.gstatic?com/images,q=tbn%3AANd9GcRU6FVly4jMTD3AKB_sHxqPofJVQwqqUj5peEvgA1H4XegM3uJ7&usqp=CAU", IsRecent = true}. new Model,Music { Title = "music", Artist = "artist": Url = "http.//ir.vavmusic.com/download/M/Mohsen%20Ebrahimzadeh/Single/320/Mohsen%20Ebrahimzadeh%20-%20Tabe%20Eshgh%20%28Dooneh%20Dooneh%202%29,mp33": CoverImage = "https.//encrypted-tbn0.gstatic?com/images,q=tbn%3AANd9GcRm-su97lHFGZrbR6BkgL32qbzZBj2f3gKGrFR0Pn66ih01SyGj&usqp=CAU"}; }; } }
} }
When I run project, in LandingPage.xaml.g.cd got this error, if I remove any relation to viewmodel its work fine,当我运行项目时,在 LandingPage.xaml.g.cd 中出现此错误,如果我删除与 viewmodel 的任何关系,它的工作正常,
System.InvalidCastException Message=Specified cast is not valid.
System.InvalidCastException Message=指定的转换无效。
As the comment resource dictionary is=>由于评论资源字典是=>
<Application.Resources> <ResourceDictionary> <Color x:Key="GradiantStart">#858585</Color> <Color x:Key="GradiantEnd">#575757</Color> <Color x:Key="IconBackGroundColor">#d6d7d7</Color> <Color x:Key="IconBorderColor">#e62e2d</Color> <Color x:Key="IconLableColor">#fff</Color> <LinearGradientBrush x:Key="mygradiant" EndPoint="1,0"> <GradientStop Color="#E7EDF8" Offset="0.1" /> <GradientStop Color="#E3E7EE" Offset="1.0" /> </LinearGradientBrush> <LinearGradientBrush x:Key="spacegradiant" EndPoint="1,0"> <GradientStop Color="#D4420c" Offset="0.1" /> <GradientStop Color="#f3a283" Offset="1.0" /> </LinearGradientBrush> <LinearGradientBrush x:Key="PlayListGradiant" EndPoint="1,0"> <GradientStop Color="#E3E7EE" Offset="0.1" /> <GradientStop Color="#FBFBFB" Offset="1.0" /> </LinearGradientBrush> </ResourceDictionary> </Application.Resources>
Thanks @Cfun and @ ColeX - MSFT谢谢@Cfun和@ColeX - MSFT
My mistake was, deferent between Background AND BackgroundColor I try to use gradient as background color,我的错误是,在背景和背景颜色之间有所不同,我尝试使用渐变作为背景颜色,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.