1
votes

I have been working with CarouselView while using the Nuget Package Xamarin.Forms.CarouselView. I have tried to display the images as auto sliding.

XAML

<StackLayout Grid.Row="0" Grid.ColumnSpan="4">
    <cv:CarouselView x:Name="MainCarouselView" Position="{Binding PositionIndex, Mode=TwoWay}">
        <cv:CarouselView.ItemTemplate>
            <DataTemplate>
                <Image Source="{Binding .}"></Image>
            </DataTemplate>
        </cv:CarouselView.ItemTemplate>
    </cv:CarouselView >
</StackLayout>

C#

var image = new ObservableCollection<ImageSource>
{
    ImageSource.FromFile("Image1.jpg"),
    ImageSource.FromFile("Image2.jpg"),
    ImageSource.FromFile("Image3.jpg")
};

MainCarouselView.ItemsSource = image;

Device.StartTimer(TimeSpan.FromSeconds(2), (Func<bool>) (() =>
{
    MainCarouselView.Position = (MainCarouselView.Position + 1) % image.Count;
    return true;
}));

After running the app on the device or emulator I get this error:

Severity Code Description Project File Line Suppression State Error CS0433 The type 'CarouselView' exists in both 'Xamarin.Forms.CarouselView, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' and 'Xamarin.Forms.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null'

I have tried to delete the Nuget Package and tried then the CarouselView remains empty and it also does not contain the property of Position. Any help will be appreciated.

EDIT: I have solved using CarouselView.FormsPlugin Xaml:

<controls:CarouselViewControl x:Name="MainCarouselView"  Grid.Row="0" Grid.ColumnSpan="3" Position="{Binding PositionIndex, Mode=TwoWay}">
                    <controls:CarouselViewControl.ItemTemplate>
                        <DataTemplate>
                            <Image Source="{Binding .}"></Image>
                        </DataTemplate>
                    </controls:CarouselViewControl.ItemTemplate>
              </controls:CarouselViewControl >

And kept the cs code same. Thanks for the help guys.

2
the library you are using is in preview, has stayed that way years and is not indented to be completed. Xamarin is going to have a new concept for carousel. if such a beta component is giving you issues you would recommend you don't use it. It might be easier to make your own component than tackling these issues - Neville Nazerane
@NevilleNazerane I have resolved it using CarouselView.FormsPlugin. It worked perfect. - user1234
Ok then post an answer and still take what I said into consideration. Consider the possible issues you may face and the time each might take to fix. I once tried to use Carousel and after a series of issues resorted to using my own control - Neville Nazerane

2 Answers

2
votes

Use CarouselView control for Xamarin Forms https://github.com/alexrainman/CarouselView

and be sure you are following these steps:

In your iOS and Android projects call:

MainActivity.cs :

CarouselView.FormsPlugin.Android.CarouselViewRenderer.Init();

AppDelegate.cs :

CarouselView.FormsPlugin.iOS.CarouselViewRenderer.Init();

On your XAML view

xmlns:cv="clr-namespace:CarouselView.FormsPlugin.Abstractions;assembly=CarouselView.FormsPlugin.Abstractions"
1
votes

You shoudn't use Xamarin.Forms.CarouselView plugin, as it's hasn't been updated for years...For those interested, it was supposed to be an optimised successor of the CarouselPage - you can read more about it from James' blog post. For some reason it has been completely abandoned by Xamarin.Forms team.

Xamarin.Forms.CarouselView Last Updated

Currently the best solution is to use Alex's Rainman CarouselView.FormsPlugin plugin, which is just great! However, I think currently it has some issues with supporting Xamarin.Forms 3.5 (github issue)

NOTE: Depending on what exactly you want to achieve, you might want to have a look at the Sharpnado's HorizontalListView carousel layout, which I'm almost certain is based off of the Alex's package.

For the future, keep an eye on the CollectionView, which will be a base for the brand new CarouselView in the Xamarin.Forms 4.0. You can read a bit more about it (and see a preview) in this blog post

NOTE: Please avoid using CarouselPage as well as the Xamarin.Forms.CarouselView

The CarouselPage does not support UI virtualization. Therefore, performance may be affected if the CarouselPage contains too many child elements.