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
.
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).
3.145.36.221