Sharing Images with Other Devices Using NFC or with Other Registered Phone Services

,

New to the Windows Phone 8 SDK is the ShareMediaTask, which allows you to pass an image to another device via NFC, or to another person using the phone’s messaging app, or with another registered service on the phone such as OneNote.

To launch the ShareMediaTask, create a new instance of the task and set its FilePath property to the location of a file on the device, as shown:

ShareMediaTask shareMediaTask = new ShareMediaTask();
shareMediaTask.FilePath = fileName;
shareMediaTask.Show();

The ShareMediaTask works well in conjunction with other tasks, in particular the CameraCaptureTask and the PhotoChooserTask. This is demonstrated in the ShareMediaTaskViewModel in the WPUnleashed.Examples project of the downloadable sample code (see Listing 14.6).

The viewmodel contains a PhotoChooserTask that is initialized as a field of the class. When the ShareCommand is executed the PhotoChooserTask is shown. The Completed event handler uses the PhotoResult object’s OriginalFileName property to launch a new ShareMediaTask.

LISTING 14.6. ShareMediaTaskViewModel Class


public class ShareMediaTaskViewModel : ViewModelBase
{
    readonly PhotoChooserTask photoChooserTask = new PhotoChooserTask();

    public ShareMediaTaskViewModel() : base("share image")
    {
        shareCommand = new DelegateCommand(arg => photoChooserTask.Show());

        photoChooserTask.Completed += HandlePhotoChooserTaskCompleted;
    }

    void HandlePhotoChooserTaskCompleted(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            ShareMediaTask shareMediaTask = new ShareMediaTask();
            shareMediaTask.FilePath = e.OriginalFileName;
            shareMediaTask.Show();
        }
    }

    readonly DelegateCommand shareCommand;

    public ICommand ShareCommand
    {
        get
        {
            return shareCommand;
        }
    }
}


When the ShareMediaTask is shown, the built-in Share Media app is displayed (see Figure 14.33).

Image

FIGURE 14.33 The built-in Share Media app.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.140.121