Search code examples
xamarinxamarin.formscarouselvisual-studio-2019

How to use CarouselView in Visual Studio 2019 4.4+ and Xamarin.Forms?


I updated my Xamarin.Forms version from 3.4.0.1 to the latest build: 4.4.0.991477, but the CarouselView I was using before, Xamarin.Forms.CarouselView does not work anymore and I get this error:

The type 'CarouselView' exists in both 'Xamarin.Forms.CarouselView, 
Version=1.0.7.0, Culture=neutral, PublicKeyToken=null' and
'Xamarin.Forms.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null'

I've looked all over for a solution and replacement for Xamarin.Forms.CarouselView but the best I've seen is Alex Rainman's plugin, CarouselView.FormsPlugin, https://github.com/alexrainman/CarouselView, but I've read people complaining about some bugs in that plugin.

I've also heard that Xamarin Forms has its own new plugin now? https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/carouselview/ But it's very new and there's no one talking about it, not much documentation at all...

So.. for current Xamarin.Forms developers (4.4+), what are you using right now to implement CarouselViews? Is CarouselView.FormsPlugin trustworthy? (I have to update the old CarouselView for a big company so I need to make sure it's good before I push the updated version).

Edit: After uninstalling the plugin: I'm getting a Failed to resolve assmebly: 'Xamarin.Forms.CarouselView, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' error now.

I'm guessing it's because I have the line

xmlns:cv="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView" in my xaml file, but if I take it out, then my code using CarouselView

<cv:CarouselView 
  VerticalOptions="FillAndExpand" 
                   HorizontalOptions="FillAndExpand"
                   ItemsSource="{Binding ReadingList}" 
                   Position="{Binding Position}">
    <cv:CarouselView.ItemTemplate>
...

gets squiggled out

Edit 2: I figured that I needed to change cv:____ to something else because it obviously isn't using the same plugin anymore, so I tried removing the cv: from both and it worked.. is that right?


Solution

  • I figured that I needed to change cv:____ to something else because it obviously isn't using the same plugin anymore, so I tried removing the cv: from both and it worked.. is that right?

    Yes, what you did is right.

    In Xamarin.forms 4.4.0.991477, CarouselView is a class in Namespace: Xamarin.Forms and Assembly:Xamarin.Forms.Core.dll.

    So you can modify your cv___ to :

    xmlns:cv="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Core"
    

    And use it like:

    <cv:CarouselView/>
    

    Generally, it is unnecessary to use cv because the default namespace has specified that elements defined within the XAML file with no prefix refer to Xamarin.Forms classes:

    xmlns="http://xamarin.com/schemas/2014/forms"
    

    So, after you remove the cv: from both and your project worked.