200 likes | 326 Views
Launchers and Choosers. Charles Petzold www.charlespetzold.com. Agenda. Launchers Choosers Photo extras Tombstoning. Launchers, Choosers, and Tasks. Tasks target phone's built-in apps Launch Web browser, camera app, etc. Microsoft.Phone.Tasks namespace
E N D
Launchers and Choosers Charles Petzold www.charlespetzold.com
Agenda • Launchers • Choosers • Photo extras • Tombstoning
Launchers, Choosers, and Tasks • 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
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 = "jeffpro@foo.com"; task.Cc = "jeffpro@bar.com"; task.Body = "This is a test"; task.Subject = "Test Message"; task.Show();
Launching a WebBrowserTask WebBrowserTasktask = new WebBrowserTask(); task.URL = "http://www.nasa.gov"; task.Show();
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; ... } }
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 • Extras.xml file registers app for photo extras • Must be named Extras.xml • Build action must be "Content" <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" BitmapImagebitmap = new BitmapImage(); bitmap.SetSource(picture.GetImage()); Photo.Source = new WriteableBitmap(bitmap); }
Tombstoning • Some tasks may not cause tombstoning • CameraCaptureTask • EmailAddressChooserTask • MediaPlayerLauncher • PhoneNumberChooserTasks • PhotoChooserTask • Other tasks do cause app to be tombstoned • All apps should support tombstoning!
Completed Events • Completed events fire before OnNavigatedTo • If tombstoned data is required in Completed event handler, use application state, not page state <Start App> Launching OnNavigatedTo <Launch PhotoChooserTask> OnNavigatedFrom Deactivated <Return from PhotoChooserTask> 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"]; ... } }
Questions? Charles Petzold www.charlespetzold.com