Charles Petzold 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 or place phone call Others are activated with choosers –Launches app and returns data –e.g., snap a photo or choose an address Launchers, Choosers, and Tasks
Launcher Tasks ClassDescription ComposeTask Launches the 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 = " "; task.DisplayName = "Wintellect"; task.Show();
Launching an Task ComposeTask task = new ComposeTask(); task.To = task.Cc = task.Body = "This is a test"; task.Subject = "Test Message"; task.Show();
Launching a WebBrowserTask WebBrowserTask task = new WebBrowserTask(); task.URL = " task.Show();
Chooser Tasks ClassDescription CameraCaptureTask Launches the camera app and returns a photo AddressChooserTask Allows the user to choose an 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 Save AddressTask Allows the user to save an address in the contacts list (returns completion indicator but no data) SavePhoneNumberTask Allows 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 (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 (OnSelectionCompleted); _task.Show();... private void OnSelectionCompleted(object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { Stream photo = e.ChosenPhoto;... }
demo Launchers 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 true
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); }
demo Photo Extras
Some tasks may not cause tombstoning –CameraCaptureTask – AddressChooserTask –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 Launching OnNavigatedTo OnNavigatedFrom Deactivated Activated PhotoChooserTask.Completed OnNavigatedTo Launching OnNavigatedTo OnNavigatedFrom Deactivated Activated PhotoChooserTask.Completed OnNavigatedTo
This Works // PhotoChooserTask.Completed event handler private 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 handler private void OnSelectionCompleted(object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { // Retrieve tombstoned data from page state int index = (int)this.State["Index"];... }
Charles Petzold Questions?