Google Windows 10 #UWP Apps: Now Calendar Controls are available for Phone, beginners tutorials (C#-XAML) | SubramanyamRaju Xamarin & Windows App Dev Tutorials

Saturday, 5 March 2016

Windows 10 #UWP Apps: Now Calendar Controls are available for Phone, beginners tutorials (C#-XAML)

Introduction:
In prev post i was explained about how to use TimePicker & DatePicker controls in WinRT windows phone 8.1.But the DatePicker is optimized for picking a known date, such as a date of birth, where the context of the calendar is not important. Fortunately from Windows 10 OS(version 10.0.10240.0) Microsoft introduce CalendarView & CalendarDatePicker controls, so these controls can gives you a standardized way to let users view and interact with a calendar. 

Requirements:
  • This article is targeted for windows 10 Universal Windows Platform(Version 1511) , so make sure you’ve downloaded and installed the latest Windows 10 SDK from here.
  • We no longer need a developer license with Windows 10. But we must Enable Developer Mode to your device for development.
  • This post assumes you’re using Microsoft Visual Studio 2015 or Later.
  • This article is developed on windows 8.1 machine. So Visual Studio settings may differ from windows 10 OS VS.
Note: Windows 10 SDK works best on the Windows 10 operating system. And it is also supported on: Windows 8.1, Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2, but not all tools are supported on these operating systems.

Description:
This article can explain you about below topics
1. How to create new project in Windows 10?
2. How to use CalendarView control in Windows 10?
3. How to use CalendarDatePicker control in Windows 10?
4. How to run Windows 10 UWP app in emulator?

1. How to create new project in Windows 10?

Before to use calendar controls, first we need to create new project. 
  • Launch Visual Studio 2015
  • On the File menu, select New > Project.
  • The New Project dialog appears. The left pane of the dialog lets you select the type of templates to display.In the left pane, expand Installed > Templates > Visual C# > Windows, then pick the Universal template group. The dialog's center pane displays a list of project templates for Universal Windows Platform (UWP) apps.
  • In the center pane, select the Blank App (Universal Windows) template.The Blank App template creates a minimal UWP app that compiles and runs, but contains no user-interface controls or data. You add controls to the app over the course of this tutorial.In the Name text box, type "UWPCalenderControl".Click OK to create the project.


Visual Studio creates your project and displays it in the Solution Explorer.

2. How to use CalendarView control in Windows 10?
Now its time to use calendar controls and from windows 10 OS we have an in-built class called CalendarView, which can provide a standardized way to let users view and interact with a calendar. If you need to let a user select multiple dates, you must use a CalendarView.
Okay now lets start to use CalendarView in our MainPage.xaml like below:
  1. <CalendarView x:Name="MyCalendarView DisplayMode="Month" SelectionMode="Multiple" CalendarItemBackground="LightBlue" BorderBrush="Orange" BorderThickness="3"/>  
In above code 
SelectionMode property is set to Multiple to let a user select multiple dates. DisplayMode property is useful to set calendar startup view and it is having three values(Month,Year,Decade). By default, it starts with the month view open.


So when users click the header in the month view to open the year view, and click the header in the year view to open the decade view. Users pick a year in the decade view to return to the year view, and pick a month in the year view to return to the month view. The two arrows to the side of the header navigate forward or backward by month, by year, or by decade.
And in C# code we can also set selected date, MinDate & MaxDate like below:
  1. MyCalendarView.SelectedDates.Add(new DateTime(2016, 3, 11));// Set selected date is March 3rd 2016);  
  2. MyCalendarView.MinDate = new DateTime(2016, 2, 28);//Min date is FEB 28th 2016  
  3. MyCalendarView.MaxDate = DateTime.Now.AddMonths(3); //Max date is 3 months from current date
  4. MyCalendarView.NumberOfWeeksInView = 6;//Month view show 6 weeks at a time  
3. How to use CalendarDatePicker control in Windows 10?
The CalendarView gives you a standardized way to let users view and interact with a calendar. If you need to let a user select multiple dates, you must use a CalendarView. If you need to let a user pick only a single date and don’t need a calendar to be always visible, consider using a CalendarDatePicker or DatePicker control.  So CalendarDatePicker can represents a control that allows a user to pick a date from a calendar display.
Okay now lets start to use CalendarDatePicker in our MainPage.xaml like below:
  1. <CalendarDatePicker x:Name="MyCalendarDatePicker"/>  
But we can also add more below properties to CalendarDatePicker
  • customize Header
  • PlaceholderText
  • We can also set DateFormat
  • DisplayMode of calendar(Month, Year, Decade)

  1. <CalendarDatePicker x:Name="MyCalendarDatePicker" Grid.Row="0" DisplayMode="Month" IsCalendarOpen="True" DateChanged="MyCalendarDatePicker_DateChanged" DateFormat = "{}{dayofweek.full}‎, ‎{month.full}‎ ‎{day.integer}‎, ‎{year.full}" PlaceholderText="Choose your date">  
  2.                 <!--Customising CalendarDatePicker HeaderTemplate -->  
  3.                 <CalendarDatePicker.HeaderTemplate>  
  4.                     <DataTemplate>  
  5.                         <TextBlock FontSize="18" FontWeight="Bold" Foreground="Green">  
  6.                             <Underline>  
  7.                                 Windows 10 Calendar control  
  8.                             </Underline>  
  9.                         </TextBlock>  
  10.                     </DataTemplate>  
  11.                 </CalendarDatePicker.HeaderTemplate>  
  12.             </CalendarDatePicker>  

And in C# code we can also set selected date, MinDate & MaxDate like below:
  1. MyCalendarDatePicker.Date = new DateTime(2016, 3, 11);//Selected date is March 3rd 2016  
  2. MyCalendarDatePicker.MinDate = new DateTime(2016, 2, 28);//FEB 28th 2016  
  3. MyCalendarDatePicker.MaxDate = DateTime.Now.AddMonths(3);//Max date is 3 months from current date  

We can get selected date of calendar view in 'DateChanged' event and in below code I am trying to display selected date in ContentDialog.
  1. private void MyCalendarDatePicker_DateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)  
  2.     {  
  3.         //Checking selected date is null  
  4.         if (args.NewDate != null)  
  5.             {  
  6.                 /* Getting selected new date value*/  
  7.                 DisplayDate(args.NewDate.Value.ToString());  
  8.             }  
  9.      }  
  10. private async void DisplayDate(string SelectedDate)  
  11.      {  
  12.           ContentDialog noWifiDialog = new ContentDialog()  
  13.           {  
  14.                 Title = "Windows 10 Calendar Control",  
  15.                 Content = "Selected date is: " + SelectedDate,  
  16.                 PrimaryButtonText = "Ok"  
  17.           };  
  18.             noWifiDialog.Margin = new Thickness(0, 100, 0, 0);  
  19.             await noWifiDialog.ShowAsync();  
  20.       }  
4. How to run Windows 10 UWP app in emulator?
Now its time to test our sample, so follow below steps to run the app.
Step 1: In addition to the options to Debug on a desktop device, Visual Studio provides options for deploying and debugging your app on a physical mobile device connected to the computer, or on a mobile device emulator. You can choose among emulators for devices with different memory and display configurations like below
Note: 
1. It's a good idea to test your app on a deivce with a small screen and limited memory, so use the Emulator 10.0.10240.0 WVGA 4 inch 512MB option.
2. If you want to test our app in physical device, you must Register your Windows Phone device for development.
3. If you already have .APPx file, you can deploy it from WinAppDeployCmd.exe tool



After selecting emulator/device, From the Debug menu, click Start Debugging or Press F5. Then we can found below screen on emulator/device:

Important Notes:

1. A user can clear the selected date by clicking the selected date in the calendar view to deselect it.
2. By default, the minimum date shown in the CalendarView is 100 years prior to the current date, and the maximum date shown is 100 years past the current date. You can change the minimum and maximum dates that the calendar shows by setting the MinDate and MaxDate properties.
3. By default, the month view shows 6 weeks at a time. You can change the number of weeks shown by setting the NumberOfWeeksInView property. The minimum number of weeks to show is 2; the maximum is 8.
4. This sample will be work on all window 10 devices OS devices. And this sample was tested in Emulator & Lumia 735 device with Windows 10 OS (version 1511).
5. Windows 10 SDK works best on the Windows 10 operating system. And it is also supported on: Windows 8.1, Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2, but not all tools are supported on these operating systems
6. This article is developed on windows 8.1 OS Machine with 64 bit. 

Custom ClipBoard
FeedBack Note:
Please share your thoughts,what you think about this post,Is this post really helpful for you?I always welcome if you drop comments on this post and it would be impressive.

Follow me always at  
Have a nice day by  :)

5 comments:

Search Engine Submission - AddMe