20
Charles Petzold www.charlespetzold.com Launchers and Choosers

Charles Petzold Launchers and Choosers

Embed Size (px)

Citation preview

Charles Petzoldwww.charlespetzold.com

Launchers and Choosers

Agenda

• Launchers• Choosers• Photo extras• Tombstoning

• Tasks target phone's built-in apps– Launch Web browser, camera app, etc.– Microsoft.Phone.Tasks namespace

• Some tasks are activated with launchers– Launches app but doesn't return data– e.g., compose e-mail or place phone call

• Others are activated with choosers– Launches app and returns data– e.g., snap a photo or choose an e-mail address

Launchers, Choosers, and Tasks

Launcher Tasks

Class Description

EmailComposeTask Launches the e-mail app

MarketPlaceDetailTask Launches the marketplace app showing a product detail

MarketPlaceHubTask Launches the marketplace app showing the hub

MarketPlaceReviewTask

Launches the marketplace app showing a product review

MarketPlaceSearchTask

Launches the marketplace app showing search results

MediaPlayerLauncher Launches the media player app

PhoneCallTask Launches the phone app

SearchTask Launches the search app

SmsComposeTask Launches the text-messaging app

WebBrowserTask Launches the Web browser

Launching a Phone Task

PhoneCallTask task = new PhoneCallTask();task.PhoneNumber = "8659685528";task.DisplayName = "Wintellect";task.Show();

Launching an E-Mail Task

EmailComposeTask task = new EmailComposeTask();task.To = "[email protected]";task.Cc = "[email protected]";task.Body = "This is a test";task.Subject = "Test Message";task.Show();

Launching a WebBrowserTask

WebBrowserTask task = new WebBrowserTask();task.URL = "http://www.nasa.gov";task.Show();

Chooser Tasks

Class Description

CameraCaptureTask Launches the camera app and returns a photo

EmailAddressChooserTask

Allows the user to choose an e-mail address from the contacts list

PhoneNumberChooserTask

Allows the user to choose a phone number from the contacts list

PhotoChooserTask Allows the user to choose a photo from the photos app

SaveEmailAddressTaskAllows the user to save an e-mail address in the contacts list (returns completion indicator but no data)

SavePhoneNumberTaskAllows the user to save a phone number in the contacts list (returns completion indicator but no data)

Snapping a Photo

private CameraCaptureTask _task; ..._task = new CameraCaptureTask();_task.Completed += new EventHandler<PhotoResult>(OnCaptureCompleted);_task.Show(); ...private void OnCaptureCompleted(object sender, PhotoResult e){ if (e.TaskResult == TaskResult.OK) { Stream photo = e.ChosenPhoto; ... }}

Choosing a Photo

private PhotoChooseTask _task; ..._task = new PhotoChooserTask();_task.Completed += new EventHandler<PhotoResult>(OnSelectionCompleted);_task.Show(); ...private void OnSelectionCompleted(object sender, PhotoResult e){ if (e.TaskResult == TaskResult.OK) { Stream photo = e.ChosenPhoto; ... }}

demoLaunchers and Choosers

Photo Extras

• Applications invoked through "extras…" item in picture library menu– Item only appears if one

or more extras are installed

• Work as stand-alone apps, too

• Extras.xml file registers app for photo extras– Must be named Extras.xml– Build action must be "Content"

Extras.xml

<Extras> <PhotosExtrasApplication> <Enabled>true</Enabled> </PhotosExtrasApplication></Extras>

Retrieving a Photo

protected override void OnNavigatedTo(NavigationEventArgs e){ if (NavigationContext.QueryString.ContainsKey("token")) { // If app was invoked through Extras menu, grab the photo MediaLibrary library = new MediaLibrary(); Picture picture = library.GetPictureFromToken (NavigationContext.QueryString["token"]);

// Display the photo in XAML Image object named "Photo" BitmapImage bitmap = new BitmapImage(); bitmap.SetSource(picture.GetImage()); Photo.Source = new WriteableBitmap(bitmap);}

demoPhoto Extras

• Some tasks may not cause tombstoning– CameraCaptureTask– EmailAddressChooserTask– MediaPlayerLauncher– PhoneNumberChooserTasks– PhotoChooserTask

• Other tasks do cause app to be tombstoned• All apps should support tombstoning!

Tombstoning

• Completed events fire before OnNavigatedTo

• If tombstoned data is required in Completed event handler, use application state, not page state

Completed Events

<Start App>LaunchingOnNavigatedTo

<Launch PhotoChooserTask>OnNavigatedFromDeactivated

<Return from PhotoChooserTask>ActivatedPhotoChooserTask.CompletedOnNavigatedTo

This Works

// PhotoChooserTask.Completed event handlerprivate void OnSelectionCompleted(object sender, PhotoResult e){ if (e.TaskResult == TaskResult.OK) { // Retrieve tombstoned data from application state int index = (int)PhoneApplicationService.Current.State["Index"]; ... }}

This Does Not

// PhotoChooserTask.Completed event handlerprivate void OnSelectionCompleted(object sender, PhotoResult e){ if (e.TaskResult == TaskResult.OK) { // Retrieve tombstoned data from page state int index = (int)this.State["Index"]; ... }}

Charles Petzoldwww.charlespetzold.com

Questions?