Implementing appointment provider activation in a Windows Phone Silverlight 8.1 app
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Starting with Windows Phone 8.1, your Windows Phone app can create and manage calendars that the user can view in the built-in Calendar app and even other apps if you allow it. When you create your app calendars, you can choose whether you want the system to show details for appointments in the calendar or if you want your app to be launched to show details for the appointment. If your app is a Windows Phone Silverlight 8.1 app, the code you write to support being activated to show appointment details is different than it is for a Windows Phone Store app. This topic shows you how to modify a Silverlight 8.1 to support appointment provider activation. Other aspects of implementing app calendars are the same for both app types. For more information on implementing this feature, see Quickstart: Managing app calendars (Windows Phone Store apps using C#/VB/C++ and XAML).
The following steps show you how to modify a Silverlight 8.1 app to support appointment provider activation.
Add the appointments provider extension to your app manifest
The Package.appmanifest file is used to tell the system that your app supports appointment provider activation. Add the following Extension element to the to your manifest file. You will need to add this code manually, not through the designer. To do this, in Solution Explorer, right-click on Package.appmanifest and select View code. Then paste the following XML into your Extensions element. If the Extensions element doesn’t already exist, create one inside the Application tag.
<m2:Extension Category="windows.appointmentsProvider">
<m2:AppointmentsProvider>
<m2:AppointmentsProviderLaunchActions>
<m3:LaunchAction Verb="showAppointmentDetails" />
</m2:AppointmentsProviderLaunchActions>
</m2:AppointmentsProvider>
</m2:Extension>
Important Note: |
|---|
You must also set the SummaryCardView property of your app calendar to AppointmentSummaryCardView.App for your app to be launched to show appointment details. |
Add using directives to
The rest of the code in this topic is placed in the your app’s App.xaml.cs file, where the app lifetime events are located. In order to access the APIs you should include the following namespaces in the file.
using Windows.ApplicationModel.Activation; using Windows.ApplicationModel.Appointments.AppointmentsProvider;
Declare a class variable to store the appointment activation event args
Declare a class variable to store an AppointmentsProviderShowAppointmentDetailsActivatedEventArgs so that you can use it from different methods within the file.
public AppointmentsProviderShowAppointmentDetailsActivatedEventArgs ShowApptDetailsEventArgs { get; private set; }
Implement the Launching event handler
When the system launches your app to show appointment details, the Launching event is raised. In the handler for this event, check to see if the event args is an instance of PhoneAppointmentsProviderLaunchingEventArgs. If it is, check to see what the Verb property of the IAppointmentsProviderActivatedEventArgs is. If that value is ShowAppointmentDetails, then store the args object in the class variable we created in the previous step.
private void Application_Launching(object sender, LaunchingEventArgs e) { if (e is PhoneAppointmentsProviderLaunchingEventArgs) { var apptArgs = e as PhoneAppointmentsProviderLaunchingEventArgs; if (apptArgs.AppointmentsProviderActivatedEventArgs.Verb == AppointmentsProviderLaunchActionVerbs.ShowAppointmentDetails) { ShowApptDetailsEventArgs = apptArgs.AppointmentsProviderActivatedEventArgs as AppointmentsProviderShowAppointmentDetailsActivatedEventArgs; } else { Terminate(); } } }
Caution: |
|---|
If your app is suspended when the user taps on an appointment owned by your app, the suspended instance of your app is terminated and a new instance is launched to handle the activation. |
Create a UriMapper
Next, define a new class that inherits from the UriMapperBase class. You implement this class to map a requested Uri, which for this scenario will be the one launched by the System, and optionally navigate to a different page instead, in this case it will be the page in your app that shows appointment details. In the MapUri(Uri) method, check to see if the AppointmentsProviderShowAppointmentDetailsActivatedEventArgs is null. If it’s not, then you know that this is an appointment details activation. Get the details of the appointment to be shown out of the provided args object and navigate to a new Uri, passing in the details as query string parameters. Be sure to set the AppointmentsProviderShowAppointmentDetailsActivatedEventArgs to null so that subsequent navigations won’t be redirected.
public class CustomUriMapper : UriMapperBase { public override Uri MapUri(Uri uri) { if ((Application.Current as App).ShowApptDetailsEventArgs != null) { var args = (Application.Current as App).ShowApptDetailsEventArgs; var paramString = String.Format("localId={0}&instanceStartDate={1}", args.LocalId, args.InstanceStartDate); paramString = System.Net.HttpUtility.UrlEncode(paramString); (Application.Current as App).ShowApptDetailsEventArgs = null; RootFrame.Navigate(new Uri("/AppointmentDetails.xaml?" + paramString, UriKind.Relative)); } return uri; } }
Attach the UriMapper to the RootFrame
In the InitializePhoneApplication method, provided by the app template, attach the UriMapper you created to the RootFrame object. This registers your Uri mapper to be used when navigations occur in your app.
RootFrame.UriMapper = new CustomUriMapper();
Important Note: