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.
- Windows 10 (Recommended).
- Visual Studio 2015 Community Edition (It is a free software available online).
- 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.
Step 2
Now, create project “XamFormSlider_Droid”.
Choose the Target and minimum platform version for your Universal Windows Project.
Create project “XamFormSlider_UWP”.
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.
Select ->CrossPlatform-> FormXamlPage-> Give the relevant name.
Step 5
Add Slider tag with minimum and maximum value and labels in SliderDemo.Xaml.
- <StackLayout Orientation="Vertical">
- <Label/>
- <Label Text="Slider Control in Xamarin Forms Application - UWP and Android Demo" FontSize="20" />
- <Label/>
- <Label/>
- <Label/>
- <Slider x:Name="SDemo" Minimum="0" Maximum="100" VerticalOptions="Center" ValueChanged="OnSliderValueChanged" />
- <Label x:Name="lbldisp"></Label> </StackLayout>
Step 6 Add the code given below in SliderDemo.Xaml.cs.
- void OnSliderValueChanged(object sender, ValueChangedEventArgs e)
- {
- lbldisp.Text = String.Format("Slider value is {0:F1}", e.NewValue);
- }
Step 7 Open (double click) the file App.cs in the Solution Explorer-> XamFormSlider (portable) and set the Root page.
Step 8
We will test Android and UWP. Thus, we can set the Multiple Startup Projects as XamFormSlider.Droid and XamFormSlider.UWP (Universal Windows).
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.
Step 10 Deploy your app in Local Machine and the output of the XamFormSlider App is shown below.
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.