Saturday, February 7, 2015

Windows Phone 8.1 XAML: How to share images to social networks

I've had some trouble when I was trying to add a share image button on a Windows Phone 8.1 app. I started following the documentation from Microsoft which says we must use the DataTransferManager component and set the image file via the SetBitmap method but it never worked to me.

So after a few hours searching on forums I could 'mount' that solution:

Add these lines on your construtor after the this.InitializeComponent(); line.
DataTransferManager dataTransferManager = 
dataTransferManager.DataRequested += ShareTextHandler;

ShareTextHandler will be called by DataTransferManager after you call the DataTransferManager.ShowShareUI() method.
private async void ShareTextHandler(DataTransferManager sender, 
DataRequestedEventArgs e)
 DataRequest request = e.Request;
 DataRequestDeferral deferral = e.Request.GetDeferral();

 // Loading the image file we want to share
 Windows.Storage.StorageFolder storageFolder = 
 Windows.Storage.StorageFile sampleFile = 
await storageFolder.GetFileAsync("yourimagefile.png");

 //Configuring the request
 request.Data.Properties.Title = "Post title"; //Mandatory
 request.Data.SetText("Some description"); //Optional
 request.Data.SetStorageItems(new List() { file });


Add the DataTransferManager.ShowShareUI() method on your button click event or whatever as you want it to be called.
private void OnClick_ShareButton(object sender, RoutedEventArgs e)

No comments:

Post a Comment