Skip to main content

Xamarin.Forms CarouselView

Xamarin.Forms has a nice control called CarouselView and it is very easy to add it to your Xamarin project. Below you can see the basic steps you need to follow in order to add it to your Xamarin project.
  • Install CarouselView Nuget package to your all projects (PCL, Android, iOS and Windows)
  • As the CarouselView is in a separate assembly, add CarouselView's namespace in root of your Xaml page and use it in your page like this;
 <?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="XYZ.Mobile.App.Controls.ValidationControls.Confirmation"
             xmlns:valueconverters="clr-namespace:XYZ.Mobile.App.ValueConverters"
             xmlns:cv="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView">

  <StackLayout Grid.Row="1"
                 Orientation="Vertical">

      <cv:CarouselView x:Name="ConfirmationQuestionsCarousel"
                       ItemsSource="{Binding ConfirmationQuestions}">
        <cv:CarouselView.ItemTemplate>
          <DataTemplate>
          <!--You can now add other Xamarin controls in to your CarouselView-->
            <Grid>
              <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
                <RowDefinition/>
                <RowDefinition Height="50"/>
              </Grid.RowDefinitions>

              <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
              </Grid.ColumnDefinitions>

              <Label Grid.Row="0"
                     Grid.ColumnSpan="2"
                     Text="SOME TEXT"
                     FontAttributes="Bold" />
              <Label Grid.Row="1"
                     Grid.ColumnSpan="2"
                     Text="{Binding Question}"/>

              <Button Grid.Row="2"
                      Grid.Column="0"
                      Text="No"
                      StyleId="No"
                      CommandParameter="false"
                      Command="{Binding ToggleAgree}"
                      Clicked="OnQuestionAnswered"
                      BackgroundColor="{Binding Agreed, Converter={StaticResource BoolToToggleButtonColorConverter}, ConverterParameter='Invert'}"/>
              <Button Grid.Row="2"
                      Grid.Column="1"
                      Text="Yes"
                      StyleId="Yes"
                      CommandParameter="true"
                      Command="{Binding ToggleAgree}"
                      Clicked="OnQuestionAnswered"
                      BackgroundColor="{Binding Agreed, Converter={StaticResource BoolToToggleButtonColorConverter}}"/>

            </Grid>
          </DataTemplate>
        </cv:CarouselView.ItemTemplate>
      </cv:CarouselView>
    </StackLayout>
</ContentView>
  • When I used Xamarin.Forms's CarouselView for the first time, I experienced some problems while getting the Count of the CarouselView item. I needed this count information in order to swipe to the next item correctly. Whenever I tried to get the ConfirmationQuestionsCarousel.Count info, I got "Unknown Member" error, so I eventually used the following code in order to get the count information.
         private void OnQuestionAnswered(object sender, EventArgs args)
        {
            var buttonClicked = sender as Button;
            var buttonClickedAnswer = buttonClicked.StyleId;
            
            // Ugly way to get the count
            //var s = new List<object>(ConfirmationQuestionsCarousel.ItemsSource.Cast<object>()).Count;
            // Better way to get the count
            int count = 0;

            foreach (var item in ConfirmationQuestionsCarousel.ItemsSource)
            {
                count++;
            }

            // This is to set the Carosel's Position - this is unfinished code, I put it here only as an example
            ConfirmationQuestionsCarousel.Position = 3;
        }
  • That is all, happy coding.

Comments

Popular posts from this blog

How to fix Umbraco v9 "Boot Failed : Umbraco failed to boot, if you are the owner of the website please see the log file for more details." error

If you have started working with Umbraco v9 and done your first Azure deployment to one of your testing environments, there is a possibility that you might see the following "Boot Failed" error. Error: Checking the logs In order to understand the problem, you should check the Umbarco log file.  The default location for this file is umbraco/Logs and this file contains the Machine name, along with the date information. You can reach this file via Azure's Kudu Service  or alternatively, you can get download your Azure App Service's publish profile and connect your App Service via an FTP application, i.e. FileZilla. See the FileZilla screen below; Once you get your log file, you can download it to your local machine and open it with a text editor, i.e. Notepad++. When you open it, you will see all logs, including the error message. Please be aware, as most things with Umbraco, logging is also customizable, so you can either use the default Umbraco logging which is Micros

How to use JQuery Ajax Methods for Async ASP.NET MVC Action Methods

Making repeatedly calls to async methods can be a nightmare. In this case, it makes sense to use 2 ajax methods, instead of one. Here is a simple solution to overcome this problem. See that  ajaxcalls   is emptied after the success response for the first ajax call and then the second ajax method is used to make one single call to the async action method. Hope it helps. View: @section Scripts{     < script type ="text/javascript">         var smartDebitObject = new Object();         smartDebitObject.MembershipNumber = $( "#MembershipNumber" ).val();         smartDebitObject.ProfileId = $( "#ProfileId" ).val();         smartDebitObject.FirstName = $( "#FirstName" ).val();         smartDebitObject.LastName = $( "#LastName" ).val();         smartDebitObject.AddressLine1 = $( "#AddressLine1" ).val();         smartDebitObject.Postcode = $( "#Postcode" ).val();         smartDebitObject

Umbraco lifehack: How to setup anchor/jump links in Umbraco without making code changes

Today one of my clients created a ticket asking whether it is possible to set up anchor/jump links for an Umbraco content page so that they could link different sections of the page together. This is a common scenario for most Content editors and it should be super easy with any CMS platform, right? Houston, we got a problem! Now an anchor or page jump link is a special URL that takes you to a specific place on a page and it normally requires a unique HTML element ID to link different parts of a page.  When I investigated my issue, I realised that the page sections of the Umbraco page didn't have unique IDs.  View Source Code comes to the rescue In order to resolve the issue without any code changes, I decided to update the content using the built-in View Source Code feature of Umbraco CMS. This feature is available on built-in property editors, like Rich Text Editor and is super handy for checking the code that Umbraco generates for your Front-end website and making minor adju