Code Snippets for doing common tasks on your Windows Phone 7
This post is all about the code snippets for doing certain things on your Windows Phone you always wanted to do. When you want to write a piece of code to perform a task on your phone, you need to have a understanding of Launchers & Choosers from the Microsoft.Phone.Tasks Namespace. So, let me explain it in few lines.
Launcher is an API that launches one of the built-in applications on your phone and does nothing else. (Launch media player, maps, SMS Compose etc.)
Unlike Launchers, choosers can return data and status to your application. That's the difference. (Choose a address from address book, contact from the list, choose a photo)
No more story. Launch Visual Studio, Create a small new Windows Phone Application. Add the namespace Microsoft.Phone.Tasks, fold your shirt cuffs and start here.
Launchers
Howto : Compose a SMS
Howto : Launch the browser and open a webpage
Howto : Launch the screen to turn on/off Wifi,Bluetooth or Cellular Data
Howto : Compose a Email
Howto : Share status on the social media website of your choice
Howto : Launch the web search application
Howto : Launch the Phone application. Use this to allow users to make a phone call from your application.
Howto : Launch the Mediaplayer with a config of your choice
Howto : Launch the Bing Maps with a specified/current location centered
Howto : Launch the screen to share a link on a social media of your choice
Howto : Choose Address and Display it
Howto : Capture a photo using the camera and show it to user
Howto : Allow user to Choose a email adress and display it
Howto : Allow user to choose a photo and display it
Howto : Allow user to save a ringtone from your application
Howto : Allow the user to save a contact from the application
Howto : Allow the user to save a phone number to new or existing contact
Launcher is an API that launches one of the built-in applications on your phone and does nothing else. (Launch media player, maps, SMS Compose etc.)
Unlike Launchers, choosers can return data and status to your application. That's the difference. (Choose a address from address book, contact from the list, choose a photo)
No more story. Launch Visual Studio, Create a small new Windows Phone Application. Add the namespace Microsoft.Phone.Tasks, fold your shirt cuffs and start here.
Launchers
Howto : Compose a SMS
SmsComposeTask smsTask = new SmsComposeTask(); smsTask.To = "9886190002"; smsTask.Body = "This is my new SMS"; smsTask.Show();
Howto : Launch the browser and open a webpage
WebBrowserTask task = new WebBrowserTask(); task.Uri = new Uri("http://google.com", UriKind.Absolute); task.Show();
Howto : Launch the screen to turn on/off Wifi,Bluetooth or Cellular Data
ConnectionSettingsTask cstask = new ConnectionSettingsTask(); cstask.ConnectionSettingsType = ConnectionSettingsType.WiFi; cstask.Show(); //Instead of WiFi, you can also launch AirplaneMode, BlueTooth or Cellular settings screen //by setting the appropriate ConnectionSettingsType in the above code.
Howto : Compose a Email
EmailComposeTask ectask = new EmailComposeTask(); ectask.Subject = "your email subject"; ectask.Body = " body of your email"; ectask.To = "john@gmail.com"; ectask.Cc = "frank@gmail.com"; ectask.Bcc = "david@gmail.com"; ectask.Show();
Howto : Share status on the social media website of your choice
ShareStatusTask task = new ShareStatusTask(); task.Status = "I am having my dinner now"; task.Show();
Howto : Launch the web search application
SearchTask stask = new SearchTask(); stask.SearchQuery = "angry birds"; stask.Show();
Howto : Launch the Phone application. Use this to allow users to make a phone call from your application.
PhoneCallTask pctask = new PhoneCallTask(); pctask.PhoneNumber = "9886123456"; pctask.DisplayName = "James"; pctask.Show();
Howto : Launch the Mediaplayer with a config of your choice
MediaPlayerLauncher mpLaunchertask = new MediaPlayerLauncher(); mpLaunchertask.Media = new Uri("tomjerry.wmv", UriKind.Relative); mpLaunchertask.Location = MediaLocationType.Data; mpLaunchertask.Controls = MediaPlaybackControls.Pause | MediaPlaybackControls.Stop; mpLaunchertask.Orientation = MediaPlayerOrientation.Landscape; mpLaunchertask.Show();
Howto : Launch the Bing Maps with a specified/current location centered
BingMapsTask task = new BingMapsTask(); //Omit the Center property to use the user's current location. // task.Center = new GeoCoordinate(41.6124, -112.3243); task.SearchTerm = "pizza"; task.ZoomLevel = 2; task.Show();
Howto : Launch the screen to share a link on a social media of your choice
ShareLinkTask task = new ShareLinkTask(); task.Title = "Troubleshoot"; task.LinkUri = new Uri("http://abc.com/troubleshoot.aspx", UriKind.Absolute); task.Message = "Here are some great code samples for Windows Phone."; task.Show();
Choosers
Howto : Choose Address and Display it
AddressChooserTask task = new AddressChooserTask(); task.Completed+=new EventHandler(addressChooserTask_Completed); try { task.Show(); } catch (System.InvalidOperationException ex) { MessageBox.Show("An error occurred."); } void addressChooserTask_Completed(object sender, AddressResult e) { if (e.TaskResult == TaskResult.OK) { MessageBox.Show("The address for " + e.DisplayName + " is " + e.Address); } }
Howto : Capture a photo using the camera and show it to user
CameraCaptureTask task = new CameraCaptureTask(); task.Completed += new EventHandler(cameraCaptureTask_Completed); try { task.Show(); } catch (System.InvalidOperationException ex) { MessageBox.Show("An error occurred."); } void cameraCaptureTask_Completed(object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { //Code to display the photo on the page in an image control named myImage. System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage(); bmp.SetSource(e.ChosenPhoto); myImage.Source = bmp; } }
Howto : Allow user to Choose a email adress and display it
EmailAddressChooserTask task = new EmailAddressChooserTask(); task.Completed += new EventHandler(emailAddressChooserTask_Completed); try { task.Show(); } catch (System.InvalidOperationException ex) { MessageBox.Show("An error occurred."); } void emailAddressChooserTask_Completed(object sender, EmailResult e) { if (e.TaskResult == TaskResult.OK) { MessageBox.Show("The email for " + e.DisplayName + " is " + e.Email); } }
Howto : Allow user to Choose a Phone Number and display it
PhoneNumberChooserTask task; task = new PhoneNumberChooserTask(); task.Completed += new EventHandler(phoneNumberChooserTask_Completed); try { task.Show(); } catch (System.InvalidOperationException ex) { MessageBox.Show("An error occurred."); } void phoneNumberChooserTask_Completed(object sender, PhoneNumberResult e) { if (e.TaskResult == TaskResult.OK) { MessageBox.Show("The phone number for " + e.DisplayName + " is " + e.PhoneNumber); } }
Howto : Allow user to choose a photo and display it
PhotoChooserTask task = new PhotoChooserTask(); task.Completed += new EventHandler(photoChooserTask_Completed); try { task.Show(); } catch (System.InvalidOperationException ex) { MessageBox.Show("An error occurred."); } void photoChooserTask_Completed(object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { // display the photo on the page in an image control named imgctrl. System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage(); bmp.SetSource(e.ChosenPhoto); imgctrl.Source = bmp; } }
Howto : Allow user to save a ringtone from your application
SaveRingtoneTask task; task = new SaveRingtoneTask(); task.Completed += new EventHandler(saveRingtoneChooser_Completed); try { task.Source = new Uri("appdata:/EminemTone.wma"); // task.Source = new Uri("isostore:/ EminemTone.wma"); task.DisplayName = "My custom ringtone"; task.Show(); } catch (System.InvalidOperationException ex) { MessageBox.Show("An error occurred."); } void saveRingtoneChooser_Completed(object sender, TaskEventArgs e) { switch (e.TaskResult) { //When the ringtone was saved successfully case TaskResult.OK: MessageBox.Show("Ringtone saved."); break; //When the task was cancelled by the user case TaskResult.Cancel: MessageBox.Show("Cancelled."); break; //When the ringtone could not be saved case TaskResult.None: MessageBox.Show("Could not be saved."); break; } }
Howto : Allow the user to save a contact from the application
SaveContactTask task = new SaveContactTask(); task.Completed += new EventHandler(saveContactTask_Completed); try { task.FirstName = "Jessy"; task.LastName = "Dove"; task.MobilePhone = "123456789"; task.Show(); } catch (System.InvalidOperationException ex) { MessageBox.Show("An error occurred."); } void saveContactTask_Completed(object sender, SaveContactResult e) { switch (e.TaskResult) { //When the contact was saved successfully case TaskResult.OK: MessageBox.Show("Contact saved."); break; //When the task was cancelled by the user case TaskResult.Cancel: MessageBox.Show("Save cancelled."); break; //When the contact could not be saved case TaskResult.None: MessageBox.Show("Contact could not be saved."); break; } }
Howto : Allow the user to save a phone number to new or existing contact
SavePhoneNumberTask task = new SavePhoneNumberTask(); task.Completed += new EventHandler(savePhoneNumberTask_Completed); try { task.PhoneNumber = "123456789"; task.Show(); } catch (System.InvalidOperationException ex) { MessageBox.Show("An error occurred."); } void savePhoneNumberTask_Completed(object sender, TaskEventArgs e) { switch (e.TaskResult) { //When the number was saved successfully case TaskResult.OK: MessageBox.Show("Phone number saved."); break; //When the task was cancelled by the user case TaskResult.Cancel: MessageBox.Show("Save cancelled."); break; //When the number could not be saved case TaskResult.None: MessageBox.Show("Phone number could not be saved."); break; } }