Introduction
In this article we will learn about the Share Media Task. This task enables the user to share an image from the app itself to various social media and other sharing platforms. It can be a very useful feature for all the files and media based apps. With this API only images can be shared. Let's see how to use it.
Share Media Task
This is a kind of launcher provided by Windows Phone to launch the share screen from an app so that the user can share the media file. The task expects one parameter for its proper working and that is a file path.
Before using this task we need to include the following namespace:
using Microsoft.Phone.Tasks;
Use the following procedure to use this task.
- Create a new instance of the Share Media Task.
Microsoft.Phone.Tasks.ShareMediaTask smt = newShareMediaTask();
- Next is to set the file path property of this task.
smt.FilePath = path;
- Finally, show this task by calling the show method.
smt.Show();
Properties that we can set for this task are as follows:
- File Path
Sets the path to the media item to be shared.
Demo
The following code demonstrates how to use the Share Media Task for sharing a media from our app. In this demo I'm capturing a photo and then sharing it. To choose an existing photo you can use the photo chooser task.
XAML
<phone:PhoneApplicationPage
x:Class="Demo.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="Demo" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="Demo" Margin="9,-7,0,0" FontSize="40" />
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel Orientation="Vertical">
<Button x:Name="btnShare" Click="btnOpen_Click" Content="Capture And share Image" HorizontalAlignment="Left" Width="436" Height="641" VerticalAlignment="Bottom" Margin="10,0,0,0"/>
</StackPanel>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
C# Code Behind
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;
using Microsoft.Phone.Tasks;
using System.Windows.Media;
namespace Demo
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void btnOpen_Click(object sender, RoutedEventArgs e)
{
startCamera();
}
private void startCamera()
{
CameraCaptureTask cct = new CameraCaptureTask();
cct.Completed += cct_Completed;
cct.Show();
}
void cct_Completed(object sender, PhotoResult e)
{
if (e.TaskResult==TaskResult.OK)
{
shareImage(e.OriginalFileName);
}
}
private void shareImage(string path)
{
Microsoft.Phone.Tasks.ShareMediaTask smt = new ShareMediaTask();
smt.FilePath = path;
smt.Show();
}
}
}
The Show method is used to launch the task.
Output