다음을 통해 공유


How to use the save contact task for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Use the save contact task to enable a user to save a contact from your application. This task launches the Contacts application.

By using Choosers, you help provide a consistent user experience throughout the Windows Phone platform. For more information, see Launchers and Choosers for Windows Phone 8.

To use the save contact task

  1. Add the following statement to your code.

    using Microsoft.Phone.Tasks;
    
    Imports Microsoft.Phone.Tasks
    
  2. Declare the task object. It must have page scope, so declare it in your page before the constructor.

    SaveContactTask saveContactTask;
    
    Dim saveContactTask As SaveContactTask
    
  3. Add the following code to your page constructor. This code initializes the task object, and identifies the method to run after the user completes the task.

    saveContactTask = new SaveContactTask();
    saveContactTask.Completed += new EventHandler<SaveContactResult>(saveContactTask_Completed);
    
    saveContactTask = new SaveContactTask()
    AddHandler saveContactTask.Completed, AddressOf saveContactTask_Completed
    
  4. Add the following code to your application wherever you need it, such as in a button click event. To test this procedure, you can put the code in the page constructor. This is the code to launch the task.

    You can pre-populate the properties, but it is not required. The user can add and edit all the properties before saving. For the full list of available properties, see SaveContactTask.

    
    
    saveContactTask.FirstName = "John";
    saveContactTask.LastName = "Doe";
    saveContactTask.MobilePhone = "2065550123";
    
    saveContactTask.Show();
    
    
    
    
    
    
    saveContactTask.FirstName = "John"
    saveContactTask.LastName = "Doe"
    saveContactTask.MobilePhone = "2065550123"
    
    saveContactTask.Show()
    
    
    
    
    
  5. Add the code for the completed event handler to your page. This code runs after the user completes the task. You can check to see whether the contact was saved successfully.

    void saveContactTask_Completed(object sender, SaveContactResult e)
    {
        switch (e.TaskResult)
        {
            //Logic for when the contact was saved successfully
            case TaskResult.OK:
                MessageBox.Show("Contact saved.");
                break;
    
            //Logic for when the task was cancelled by the user
            case TaskResult.Cancel:
                MessageBox.Show("Save cancelled.");
                break;
    
            //Logic for when the contact could not be saved
            case TaskResult.None:
                MessageBox.Show("Contact could not be saved.");
                break;
        }
    }
    
    Private Sub saveContactTask_Completed(sender As Object, e As SaveContactResult)
    
        Select Case e.TaskResult
    
            'Logic for when the contact was saved successfully
            Case TaskResult.OK
    
                MessageBox.Show("Contact saved.")
    
            'Logic for when the task was cancelled by the user
            Case TaskResult.Cancel
    
                MessageBox.Show("Save cancelled.")
    
            'Logic for when the contact could not be saved
            Case TaskResult.None
    
                MessageBox.Show("Contact could not be saved.")
        End Select
    End Sub
    

See Also

Reference

Completed

Contact

Other Resources

How to use the save email address task for Windows Phone 8

How to use the save phone number task for Windows Phone 8

How to use the save ringtone task for Windows Phone 8

Contacts and Calendar for Windows Phone 8