This is my code in xamarin content page the name of this element is 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,
System.InvalidCastException Message=Specified cast is not valid.
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>
BackgroundColor="{StaticResource spacegradiant}"
should beBackground="{StaticResource spacegradiant}"
sincespacegradiant
seems to be a Gradient not a color – CfunBackgroundColor="{StaticResource spacegradiant}"
, you have to ensurespacegradiant
is aColor
type . – ColeX - MSFT