Appointment App demonstrates how to add an Appointment to the Calendar in Windows 10.
Step 1
If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.
Step 2
Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…
Step 3
From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
Step 4
Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10 Fall Creators Update (10.0; Build 16299) and the Minimum Version to be the same.
The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates
Step 5
Once done select from the Menu, Project, then Add New Item…
Step 6
From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project
Step 7
Once in the Code View for Library.cs the following should be entered:
using System; using Windows.ApplicationModel.Appointments; using Windows.Foundation; using Windows.UI.Popups; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; public class Library { private const string app_title = "Appointment App"; public void Show(string content, string title) { IAsyncOperation<IUICommand> command = new MessageDialog(content, title).ShowAsync(); } public void New(DatePicker startDate, TimePicker startTime, TextBox subject, TextBox location, TextBox details, ComboBox duration, CheckBox allDay) { startDate.Date = DateTime.Now; startTime.Time = DateTime.Now.TimeOfDay; subject.Text = string.Empty; location.Text = string.Empty; details.Text = string.Empty; duration.SelectedIndex = 0; allDay.IsChecked = false; } public async void Add(object sender, DatePicker startDate, TimePicker startTime, TextBox subject, TextBox location, TextBox details, ComboBox duration, CheckBox allDay) { FrameworkElement element = (FrameworkElement)sender; GeneralTransform transform = element.TransformToVisual(null); Point point = transform.TransformPoint(new Point()); Rect rect = new Rect(point, new Size(element.ActualWidth, element.ActualHeight)); DateTimeOffset date = startDate.Date; TimeSpan time = startTime.Time; int minutes = int.Parse((string)((ComboBoxItem)duration.SelectedItem).Tag); Appointment appointment = new Appointment() { StartTime = new DateTimeOffset(date.Year, date.Month, date.Day, time.Hours, time.Minutes, 0, TimeZoneInfo.Local.GetUtcOffset(DateTime.Now)), Subject = subject.Text, Location = location.Text, Details = details.Text, Duration = TimeSpan.FromMinutes(minutes), AllDay = (bool)allDay.IsChecked }; string id = await AppointmentManager.ShowAddAppointmentAsync(appointment, rect, Placement.Default); if (string.IsNullOrEmpty(id)) { Show("Appointment not Added", app_title); } else { Show($"Appointment {id} Added", app_title); } } public async void Calendar(DatePicker startDate, TimePicker startTime) { await AppointmentManager.ShowTimeFrameAsync(startDate.Date, startTime.Time); } }
In the Library.cs there are using statements to include the necessary functionality. There is a Show Method that will display a MessageDialog, New is used to reset the Controls in the Application and Add is used to create an Appointment it also requires a location in which to use ShowAddAppointmentAsync with and a MessageDialog to display if this has been successful or not. CalendarAsync is used to display the Calendar in Windows 10.
Step 8
In the Solution Explorer select MainPage.xaml
Step 9
From the Menu choose View and then Designer
Step 10
The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> <TextBlock Margin="10">Start:</TextBlock> <DatePicker Name="StartDate" Margin="5"/> <TimePicker Name="StartTime" Margin="5"/> <TextBlock Margin="10">Subject:</TextBlock> <TextBox Name="Subject" Width="300" MaxLength="255" HorizontalAlignment="Left"/> <TextBlock Margin="10">Location:</TextBlock> <TextBox Name="Location" Width="300" MaxLength="32768" HorizontalAlignment="Left"/> <TextBlock Margin="10">Details:</TextBlock> <TextBox Name="Details" Width="300" MaxLength="1073741823" HorizontalAlignment="Left"/> <TextBlock Margin="10">Duration:</TextBlock> <ComboBox x:Name="Duration" SelectedIndex="0"> <ComboBoxItem Tag="30">30 Minutes</ComboBoxItem> <ComboBoxItem Tag="60">1 Hour</ComboBoxItem> </ComboBox> <CheckBox Name="AllDay" Content="All Day" Margin="10"/> </StackPanel> <CommandBar VerticalAlignment="Bottom"> <AppBarButton Icon="Page2" Label="New" Click="New_Click"/> <AppBarButton Icon="Add" Label="Add" Click="Add_Click"/> <AppBarButton Icon="Calendar" Label="Calendar" Click="Calendar_Click"/> </CommandBar>
The first block of XAML the main user interface of the Application, this features a StackPanel containing TextBlock Controls to act as Labels, plus a DatePicker and TimePicker to select when event occur and there are TextBox Controls for the Subject, Location and Details of the event with a ComboBox for Duration and a CheckBox to denote if an event occurs All Day. The second block of XAML is is the CommandBar which contains New – to reset the Control, Add to add an Appointment and Calendar to view the Calendar in Windows 10 to allow the viewing of added events.
Step 11
From the Menu choose View and then Code
Step 12
Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:
Library library = new Library(); private void New_Click(object sender, RoutedEventArgs e) { library.New(StartDate, StartTime, Subject, Location, Details, Duration, AllDay); } private void Add_Click(object sender, RoutedEventArgs e) { library.Add(sender, StartDate, StartTime, Subject, Location, Details, Duration, AllDay); } private void Calendar_Click(object sender, RoutedEventArgs e) { library.Calendar(StartDate, StartTime); }
Below the MainPage() Method an instance of the Library Class is created, then New_Click is used to call the New method to reset the Application and Add_Click calls the Add Method and Calendar_Click calls the Calendar Method in the Library Class
Step 13
That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application
Step 14
After the Application has started running you can enter details for an Appointment then use Add to add it or you can use Calendar to view any appointments added by the Application
Step 15
To Exit the Application select the Close button in the top right of the Application