Slider Control In Xamarin.Forms Application For Android And UWP

Slider control allows you to pick a numeric value by dragging your thumb along a horizontal or vertical line.

Before reading this article, please go through the article How To Create And Use XAML Content Page In Xamarin Forms Application For Android And Universal Windows Platform

After reading this article, you will learn how to add a slider control in Xamarin Forms Application for Android and Universal Windows Platform with XAML and Visual C# in cross platform application development.

The important tools are given below, which are required to develop UWP.

  1. Windows 10 (Recommended).
  2. Visual Studio 2015 Community Edition (It is a free software available online).
  3. Using Visual studio 2015 Installer, enable Xamarin (Cross Platform Mobile development and C#/.NET while installing/modifying Visual Studio 2015.

Now, we can discuss step by step app development.

Step 1


Open Visual Studio 2015 -> Start -> New Project-> Select Cross-Platform (under Visual C#-> Blank app (Xamarin.Forms Portable)-> Give the suitable name for your app (XamFormSlider) ->OK.

Visual studio

Step 2

Now, create project “XamFormSlider_Droid”.

Visual studio

Choose the Target and minimum platform version for your Universal Windows Project.

Visual studio

Create project “XamFormSlider_UWP”.

Visual studio

Step 3

Afterwards, Visual Studio creates 6 projects and displays Getting Started.XamarinPage. Now, we have to update Xamarin.Forms Reference for Portable Project and XamFormSlider_Droid project.

(Please refer How To Create And Use XAML Content Page In Xamarin Forms Application For Android And Universal Windows Platform)

Step 4

Add a XAML page for Slider Control demo. Right click XamFormSlider(Portable) project, Select ADD-> NewItem.

Visual studio

Select ->CrossPlatform-> FormXamlPage-> Give the relevant name.

Visual studio

Step 5

Add Slider tag with minimum and maximum value and labels in SliderDemo.Xaml.

  1. <StackLayout Orientation="Vertical">  
  2.     <Label/>  
  3.     <Label Text="Slider Control in Xamarin Forms Application - UWP and Android Demo" FontSize="20" />  
  4.     <Label/>  
  5.     <Label/>  
  6.     <Label/>  
  7.     <Slider x:Name="SDemo" Minimum="0" Maximum="100" VerticalOptions="Center" ValueChanged="OnSliderValueChanged" />  
  8.     <Label x:Name="lbldisp"></Label> </StackLayout>  
Visual studio

Step 6

Add the code given below in SliderDemo.Xaml.cs.
  1. void OnSliderValueChanged(object sender, ValueChangedEventArgs e)  
  2. {  
  3. lbldisp.Text = String.Format("Slider value is {0:F1}", e.NewValue);  
  4. }  
Visual studio

Step 7

Open (double click) the file App.cs in the Solution Explorer-> XamFormSlider (portable) and set the Root page.
 
Visual studio

Step 8

We will test Android and UWP. Thus, we can set the Multiple Startup Projects as XamFormSlider.Droid and XamFormSlider.UWP (Universal Windows).

Visual studio

Step 9

Change the Configuration Manager settings. Go to Build -> Configuration Manager, uncheck all the build and deploy options to the iOS, Windows, WinPhone, check the Droid and UWP.

Visual studio

Step 10

Deploy your app in Local Machine and the output of the XamFormSlider App is shown below.

Visual studio

Summary

Now, you have successfully created and tested a slider control in Xamarin.Forms Application for Cross Platform Application Development , using Visual C# and Xamarin.