I'm trying to use the CarouselView in the Xamarin project. But I can’t do it. Here are the installed packages:
Here is the xaml code:
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:FlowersStore" xmlns:cv="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView" x:Class="FlowersStore.MainPage"> <StackLayout> <Grid RowSpacing="0"> <Grid.RowDefinitions> <RowDefinition Height=".3*"/> <RowDefinition Height=".7*"/> </Grid.RowDefinitions> <cv:CarouselView ItemsSource="{Binding Zoos}" x:Name="CarouselZoos"> <cv:CarouselView.ItemTemplate> <DataTemplate> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Image Grid.RowSpan="2" Aspect="AspectFill" Source="{Binding ImageUrl}"/> <StackLayout Grid.Row="1" BackgroundColor="#80000000" Padding="12"> <Label TextColor="White" Text="{Binding Name}" FontSize="16" HorizontalOptions="Center" VerticalOptions="CenterAndExpand"/> </StackLayout> </Grid> </DataTemplate> </cv:CarouselView.ItemTemplate> </cv:CarouselView> </Grid> </StackLayout> And here is the c # code:
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using Xamarin.Forms; namespace FlowersStore { public class Zoo { public string ImageUrl { get; set; } public string Name { get; set; } } public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); LoadDataCatouselView(); } public void LoadDataCatouselView() { ObservableCollection<Zoo> Zoos = new ObservableCollection<Zoo> { new Zoo { ImageUrl = "http://content.screencast.com/users/JamesMontemagno/folders/Jing/media/23c1dd13-333a-459e-9e23-c3784e7cb434/2016-06-02_1049.png", Name = "Woodland Park Zoo" }, new Zoo { ImageUrl = "http://content.screencast.com/users/JamesMontemagno/folders/Jing/media/6b60d27e-c1ec-4fe6-bebe-7386d545bb62/2016-06-02_1051.png", Name = "Cleveland Zoo" }, new Zoo { ImageUrl = "http://content.screencast.com/users/JamesMontemagno/folders/Jing/media/e8179889-8189-4acb-bac5-812611199a03/2016-06-02_1053.png", Name = "Phoenix Zoo" } }; CarouselZoos.ItemsSource = Zoos; } } } I use Xamarin Live Player for debugging. The log on the mobile phone displays the following message: [LogEntry: Time=19.11.2018 14:54:54 +03:00, Level=Error, Title=Visualization Error, Message=The given key was not present in the dictionary. (KeyNotFoundException)]
How to fix it? Thanks.
Update 1: I replaced the code based on your advice. I used your advice. I tried to run the application on:
- Androind version: 7.1
- Emulator: Genymotion Galaxy S7 7.1.0 API 25
What it? :(
