Saturday 16 February 2013

New Launchers in Windows Phone 8

Windows Phone 8 SDK offers a set of predefined tasks that enable applications to access phone functionality (Calendar, Map, SMS, Camera and more) and perform common tasks such as saving appointments, downloading map data for offline usage, sharing a media file on social networks and more. All this is performed via different Launchers and Choosers, each exposing a different API. The difference between a Launcher and a Chooser is that choosers return data, while launchers just start an application from the phone but do not return anything.
NOTE: It is important to consider that when you start a task, a separate application is launched to complete the task and your application is tombstoned.
This article guides you through the new Tasks in Windows Phone 8 and shows how to use them.
NOTE: All Tasks that are available in Windows Phone 8 can be found in the Microsoft.Phone.Tasksnamespace. Do not forget to include this namespace when using any of the Task classes.

SaveAppointmentTask

The SaveAppointmentTask launches the built-in calendar application and prompts the user to add a new appointment to their calendar. The task API allows you to populate many of the fields of the new appointment.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private void btnLaunchAppointmentTask_Click(object sender, RoutedEventArgs e)
{
  SaveAppointmentTask saveAppointmentTask = new SaveAppointmentTask();
  DateTime currentTime = DateTime.Now;
  saveAppointmentTask.StartTime = currentTime.AddHours(1);
  saveAppointmentTask.EndTime =  currentTime.AddHours(3);
  saveAppointmentTask.Subject = "Meet John"; // appointment subject
  saveAppointmentTask.Location = "Office"; // appointment location
  saveAppointmentTask.Details = "Meet John to discuss product launch"; // appointment details
  saveAppointmentTask.IsAllDayEvent = false;
  saveAppointmentTask.Reminder = Reminder.FifteenMinutes;
  saveAppointmentTask.AppointmentStatus = Microsoft.Phone.UserData.AppointmentStatus.Busy;
  saveAppointmentTask.Show();
}
image

MapDownloaderTask

The MapDownloaderTask launches the built-in Map application settings and enables the user to download map data for offline use:
?
1
2
3
4
5
private void btnLaunchMapDownloader-Task_Click(object sender, RoutedEventArgs e)
{
    MapDownloaderTask mapDownloa-derTask = new MapDownloaderTask();
    mapDownloaderTask.Show();
}
image

MapsTask

The MapsTask launches the built-in Map application centered at the user`s current location. You can set a new location, a search term or the map zoom level using the Center, SearchTerm and ZoomLevel properties as shown in the code snippet.
?
1
2
3
4
5
6
7
8
9
private void btnLaunchMapsTask_Click(object sender, RoutedEventArgs e)
{
  MapsTask mapsTask = new Maps-Task();
  mapsTask.Center = new Geo-Coordinate(51.5171, -0.1362); // London
  // 1 - zoomed out, 20 - zoomedin
  mapsTask.ZoomLevel = 7;
  mapsTask.SearchTerm = "pizza";
  mapsTask.Show();
}
image

MapsDirectionsTask

The MapsDirectionsTask launches the built-in Map application with driving directions for getting from a starting location to a destination location. The starting and destination locations can be set as coordinates using the Start and End properties.
?
1
2
3
4
5
6
7
8
private void btnLaunchMapsDirections-Task_Click(object sender, RoutedEventArgs e)
{
    MapsDirectionsTask mapsDirectionsTask = new MapsDirectionsTask();
    mapsDirectionsTask.Start = new LabeledMapLocation("London Victoria", new Geo-Coordinate(51.495322, -0.144732));
    mapsDirectionsTask.End = new LabeledMapLocation("London Heathrow", new GeoCoor-dinate(51.471179, -0.454447));
    mapsDirectionsTask.Show();
}
image

ShareMediaTask

The ShareMediaTask launches a dialog that enables a user to share a media file. The following code snippet demonstrates how to ask the user to pick a photo and then share it.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private void btnLaunchShareMediaTask_Click(object sender, RoutedEventArgs e)
{
    PhotoChooserTask photoChooserTask = new PhotoChooserTask();
    photoChooserTask.ShowCamera = true;
    photoChooserTask.Completed += photoChooserTask_Completed;
    photoChooserTask.Show();
}
void photoChooserTask_Completed(object sender, PhotoResult e)
{
    ShareMediaTask shareMediaTask = new ShareMediaTask();
    shareMediaTask.FilePath = e.OriginalFileName;
    shareMediaTask.Show();
}

Conclusion

In this article we discussed which the new tasks in Windows Phone 8 are and how to use them. We showed how to prompt the user to add a new appointment, how to display a location or directions on a map and finally, how to share a media file.
NOTE: This article is a part of the FREE WindowsPhoneGeek Magazine. You can download the magazine as well as the he full source code here: http://windowsphonegeek.com/magazine
http://windowsphonegeek.com/articles/new-launchers-in-windows-phone-8

No comments:

Post a Comment