IT tutorials
 
Mobile
 

Windows Phone 8 : Writing Your First Phone Application - Working with the Phone

12/21/2012 6:30:08 PM
- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire

This first application is a program that can be pretty self-sufficient, but not all applications are like that. Most applications will want to interact with the phone’s operating system to work with other parts of the phone. From within your application you may want to make a phone call, interact with the user’s contacts, take pictures, and so on. The Windows Phone SDK calls these types of interactions tasks. Tasks let you leave an application (and optionally return) to perform a number of phone-specific tasks. Here is a list of some of the most common tasks:

CameraCaptureTask

EmailAddressChooserTask

EmailComposeTask

PhoneCallTask

SearchTask

WebBrowserTask

These tasks allow you to launch a task for the user to perform. In some of these tasks (e.g., CameraCaptureTask, EmailAddressChooserTask), once the task is complete the user expects to return to your application; while in others (e.g., SearchTask) the user may be navigating to a new activity (and may come back via the Back key, but may not).

Let’s start with a simple task, the SearchTask. Add a using statement to the top of the code file for Microsoft.Phone.Tasks to make sure the SearchTask class is available to our code file. Next, create an event handler for the Tap event on theEllipse. Then, inside the handler for the Tap event, you can create an instance of the SearchTask, set the search criteria, and call Show to launch the task:

...
using Microsoft.Phone.Tasks;
...
public partial class MainPage : PhoneApplicationPage
{
  // Constructor
  public MainPage()
  {
    ...

    theEllipse.Tap += theEllipse_Tap;

  }

  void theEllipse_Tap(object sender,
                      System.Windows.Input.GestureEventArgs e)
  {
    SearchTask task = new SearchTask();
    task.SearchQuery = "Windows Phone";
    task.Show();
  }

  ...
}

If you run your application, you’ll see that when you tap on the theEllipse element it will launch the phone’s Search function using the search query you supplied (as shown in Figure 1). The results you retrieve for the search query may vary as it is using the live version of Bing for search.

Figure 1. The SearchTask in action

Image

While this sort of simple task is useful, the more interesting story is being able to call tasks that return to your application. For example, let’s pick an email address from the phone and show it in our application. The big challenge here is that when we launch our application, we may get tombstoned (or deactivated). Remember that, on the phone, only one application can be running at a time. In order to have our task wired up when our application is activated (remember, it can be deactivated or even unloaded if necessary), we have to have our task at the page or application level and wired up during construction. So, in our page, create a class-level field and wire up the Completed event at the end of the constructor for it, like so:

public partial class MainPage : PhoneApplicationPage
{

  EmailAddressChooserTask emailChooser =
    new EmailAddressChooserTask();

  // Constructor
  public MainPage()
  {
    ...

    emailChooser.Completed += emailChooser_Completed;
  }

  ...
}

In the event handler, we can simply show the email chosen using the MessageBox API:

...
void emailChooser_Completed(object sender, EmailResult e)

{

  MessageBox.Show(e.Email);
}

...

Now we need to call the task. To do this, let’s highjack the event that gets called when the theEllipse element is tapped. Just comment out the old SearchTask code and add a call to the emailChooser’s Show method, like so:

...
void theEllipse_MouseLeftButtonUp(object sender,
                                  MouseButtonEventArgs e)
{
  //SearchTask task = new SearchTask();
  //task.SearchQuery = "Windows Phone";
  //task.Show();

  // Get an e-mail from the user's Contacts
  emailChooser.Show();
}
...

Once you run the application, a list of contacts will be shown and you will be able to pick a contact (and an address, if there is more than one), as shown in Figure 2. The emulator comes prepopulated with several fake contacts to test with.

Figure 2. Choosing a contact to retrieve an email address via the EmailAddressChooserTask

Image

Once the user picks the contact, the phone returns to your application. You will be returned to your application (and debugging will continue). The event handler should be called when it is returned to the application, as shown in Figure 3.

Figure 3. Showing the selected email in a MessageBox

Image
 
Others
 
- Setting Up Your Android Development Environment : Hello, Android
- Setting Up Your Android Development Environment : Setting Up Your Development Environment
- Symbian OS : Error-Handling Strategies - Escalate Errors
- Symbian OS : Error-Handling Strategies - Fail Fast
- BlackBerry Tablet Applications : Exploring the APIs - GPS
- BlackBerry Tablet Applications : Exploring the APIs - Accelerometer
- iPhone Developer : Working with View Controllers - Remembering Tab State
- iPhone Developer : Working with View Controllers - Tab Bars
- iPhone Developer : Working with View Controllers - Presenting a Custom Modal Information View
- Windows Phone 8 : Writing Your First Phone Application - Adding Code (part 3) - Using Touch
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
programming4us programming4us
 
Popular tags
 
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS