I'm trying to use CarouselView to create a horizontal ListView since a client of mine does not like the vertical ListView I have implemented in their project. I tried to follow several tutorials, but to no avail.
I have a hunch that the issue is with the nuget packages I downloaded or with the namespace I'm using in the xml. I am using Visual studio 2019 in a Xamarin Android App.
Here is a list of nuget packages I downloaded for the Carousel View:
CarouselView
CarouselView.FormsPlugin
CarouselView.FormsPlugin.Fix
Xamarin.Forms v5.0.0.2083
Here is the code in my content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:forms="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello World!" />
<forms:CarouselView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
All I added here apart from the default page you get when you create a new project is the xmlns:forms line for the namespace to use and the CarouselView tag itself.
In my code behind OnCreate, I have this:
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
CarouselViewRenderer.Init();
SetContentView(Resource.Layout.activity_main);
Again, all I added here was the Init call on the CarouselViewRenderer. I am getting the error "Error inflating class CarouselView" when SetContentView is called.
Please let me know if I'm overlooking something stupid. I can't event get Intellisense to suggest CarouselView in xml, so I'm sure there's something wrong. Maybe it cant find the tag? Which is why I'm thinking it could be a namespace issue.
What I tried:
I tried swapping around the Init call and call it in a different order, but nothing worked.
I looked for questions similar to this in the GitHub forums but no solution seems to apply to my case.
I tried changing the tag name in xml to something like CarouselVie without the 'w' and I get the same error, which means it probably isn't finding that tag to begin with.
I tried changing the namespaces and looking for different nuget packages to download but again nothing worked.
CodePudding user response:
From document What is Xamarin.Forms? ,we know that
Xamarin.Forms is an open-source UI framework. Xamarin.Forms allows developers to build Xamarin.Android, Xamarin.iOS, and Windows applications from a single shared codebase.
Xamarin.Forms allows developers to create user interfaces in XAML with code-behind in C#. These interfaces are rendered as performant native controls on each platform.
At runtime, Xamarin.Forms utilizes platform renderers to convert the cross-platform UI elements into native controls on Xamarin.Android, Xamarin.iOS and UWP. This allows developers to get the native look, feel and performance while realizing the benefits of code sharing across platforms.
So, you can use Xamarin Forms controls (CarouselView
) in forms'app,and deploy the app to your android devices or ios devices.
From above description you posted, I think you're confused about CarouselView
's usage.
Since the CarouselView
is a control of xamarin form, you should use it in xamarin forms's app rather than referencing it in Android.
For how to use CarouselView
, you can refer to the official document : Xamarin.Forms CarouselView.
And there is a sample(Xamarin.Forms - CarouselView) included in this document, you can download it and deploy it to your android device which will help you understand it better.