The visual representation of a DatePicker is very similar to the one of Entry, except that a special control for picking a date appears in place of a keyboard.
Before reading this article, please go through the following article.
Reading this article, you will learn how to use DatePicker in Xamarin.Forms application for Android and Universal Windows Platform with XAML and Visual C#.
The following important tools are required for developing UWP.
- Windows 10 (Recommended)
- Visual Studio 2015 Community Edition (It is a free software available online)
- Using Visual Studio 2015 Installer. Enable the Xamarin and C#/.NET while installing/modifying Visual Studio 2015.
Now, we can discuss step by step app development.
Step 1
Open Visual Studio 2015. Go to Start -> New Project-> Select Cross-Platform (under Visual C#-> Blank App (Xamarin.Forms Portable)-> Give a suitable name for your app (XamFormDatePick) ->OK.
Step 2
Now, create project “XamFormDatePick_Droid” …
Step 3
Choose the Target and Minimum platform versions for your Universal Windows Project.
Creating project “XamFormDatePick_UWP” ….
Step 4
After that, Visual Studio creates six projects and displays the GettingStarted.Xamarin Page. Now, we have to update the Xamarin.Forms reference for Portable Project and XamFormDatePick_Droid project.
(Please refer How To Create And Use XAML Content Page In Xamarin Forms Application For Android And Universal Windows Platform)
Step 5 - Add an XAML Page for Date Picker Demo.
Right click XamFormDatePick(Portable) project, aelect ADD-> NewItem.
Select ->CrossPlatform-> FormXamlPage-> Give the relevant name.
Step 6 For DatePicker demo, add the following name space in DatePickDemo.xaml
- xmlns: sys = "clr-namespace:System;assembly=mscorlib"
- Add Date Picker Tag and Label in DatePickDemo.xaml. <
- StackLayout >
- <
- Label x: Name = "lblTitle"
- Text = "Date Picker in Xamarin Forms - Demo"
- Font = "Large"
- HorizontalOptions = "Center"
- VerticalOptions = "CenterAndExpand" / >
- <
- DatePicker x: Name = "datepicker"
- VerticalOptions = "CenterAndExpand"
- Date = "{x:Static sys:DateTime.Now}" / >
- <
- /StackLayout>
Step 7 Open (double click) the file App.cs in the Solution Explorer-> XamFormDatePick (portable) and set the Root Page.
Step 8
We will test Android and UWP. So, we can set multiple startup projects as XamFormDatePick.Droid and XamFormDatePick.UWP (Universal Windows).
Step 9 - Change the Configuration Manager settings
Go to Build -> Configuration Manager, un-check all the "Build" and "Deploy" options except for Droid and UWP.
Step 10
Deploy your app in Local Machine. The output of the XamFormDatePick app is -
After clicking the Datepicker,
After picking the date,
Summary
Now, you have successfully created and tested DatePicker in Xamarin.Forms application using Visual C# and Xamarin.