We are now ready to set up the carousel views. We have already created the view model, so we can start to add content to it.
CarouselView and IndicatorView (which we will use later) are in preview in Xamarin.Forms. To use them, we have to add the following code to the FinishLaunching method in AppDelegate.cs for iOS and in the OnCreated method in the MainActivity.cs for Android:
global::Xamarin.Forms.Forms.SetFlags("CarouselView_Experimental", "IndicatorView_Experimental");
global::Xamarin.Forms.Forms.SetFlags("CarouselView_Experimental", "IndicatorView_Experimental");
Let's look at the steps to create the view:
-
In the contractor of the code, behind the MainView.xaml.cs file, set ViewModel to BindingContext:
public MainView()
{
InitializeComponent();
BindingContext = Resolver.Resolve<MainViewModel>();
}
- Navigate to MainView.xaml.
- Add the following code:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
...