How to Use Share Task in Windows Phone 8

Introduction

As a Smartphone user, it's always the smart way to share a picture or a piece of text on a single tap. Whether you are on Windows Phone OS or on Android, you can share your captured picture with a single tap with your social friends.

So, we have decided to use this feature from C#.

Agenda

  • Share Status as Text
  • Share a Picture as a selected Image
  • Share a Link as a Facebook path.

Procedures

Step 1

Before we proceed further, try to create a look alike UI in your project. And add a using Microsft.Phone.Task namespace in the backend of the C# page.

Share media in Windows

As per your needs, name your controls according to your comfort.

Step 2

I want to target the "Share Status" first. On the Share Status Button's click event handler use these lines of code.

  1. <code>  
  2. // Share Status  
  3. ShareStatusTask status = new ShareStatusTask();  
  4. status.Status = myStatus.Text;   
  5.   
  6. //myStatus is my TextBox control  
  7. status.Show();  
  8. </code>  
Share Status button in Windows

Above, I tried to create an instance of a ShareStatus Task of the Microsoft.Phone.Tasks namespace. And then assign it to the textbox's text to the Status property. Last, call the show() method to render it.

Step 3

We are now done with the Share Status Task. Let's move to the Share Media Task where we share a picture of from our Photo Gallery.
To do this, we need an extra task called PhotoChooserTask . It allows you to choose an image from the Photo Gallery.
  1. private void btnShareMedia_Click(object sender, RoutedEventArgs e)  
  2. {  
  3. // Browse Your Image  
  4. PhotoChooserTask choosePhoto = new PhotoChooserTask();  
  5. choosePhoto.Completed += choosePhoto_Completed;  
  6. choosePhoto.Show();  
  7. }  
  8.   
  9. void choosePhoto_Completed(object sender, PhotoResult e)  
  10. {  
  11. // Now, Share the Media  
  12. ShareMediaTask media = new ShareMediaTask();  
  13. media.FilePath = e.OriginalFileName.ToString();  
  14. media.Show();  
  15. }  
Photo ChooserTask

Here, we have initiated an instance of the PhotoChooserTask and then raised the Completed event.

And in the event handler we have assigned e.OriginalFileName to the FilePath of the Share Task.

Step 4

What if I want to share a link? Then I need to use another predefined task for this. In this step, we will discover how to do this.
  1. private void btnShareLink_Click(object sender, RoutedEventArgs e)  
  2. {  
  3. ShareLinkTask link = new ShareLinkTask();  
  4. link.LinkUri = new Uri("http://facebook.com/elmnt.abhishek", UriKind.Absolute);  
  5. link.Show();  
  6. }  
Link Uri property in Windows

In all the three tasks, we have done the same. Here, we have used the Link Uri property that accepts the URI type. So, we have ed our Facebook's profile as an absolute URI.

Conclusion

In this article, we have listed all the three predefined tasks under Microsft.Phone.Task to share. For any confusion, you can refer to the attached file.

Up Next
    Ebook Download
    View all
    Learn
    View all