In this article we are going to see how to use the Share Media Launcher task in Windows Phone Application development. This task is introduced in Windows Phone 8 SDK and supports the earlier version of the SDKs as well. Share Media Task is used to share Photo/Video files by launching the picker from the application from which we need to share the media. Let us see the steps on how to achieve this task in our Windows Phone application development. Open Visual Studio 2012 IDE and create a new Windows Phone project with a valid project name as shown in the screen below. [more]
Clicking on OK will create the project and the solution with the list of default files and folders that are required to run the application. It will take some time to create these files based on your system configuration, so once everything is ready we can see the Visual Studio IDE with the project as shown in the screen below.
Let us add some control to trigger the ShareMedia Launcher task to select the media file and share through the application, we will add a button control which will trigger the CameraCapture Task to get a picture and share the media as shown in the code below.
XAML Code:
[code:c#]<!–TitlePanel contains the name of the application and page title–>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="F5debug How to Series" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Share Media" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!–ContentPanel – place additional content here–>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="Share Photo" HorizontalAlignment="Left" Margin="69,49,0,0" Grid.Row="1" VerticalAlignment="Top" Width="340" Height="99" Click="Button_Click"/>
</Grid>
Now in the button click event add the below code which creates an instance of the Camera Capture task to get a picture which we will use as a media to share. On successful capture of the image we need to create an instance of the ShareMedia task and pass the media file that need to be shared as shown in the code below.
C# Code:
[code:c#]using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using F5debugHowto79.Resources;
using Microsoft.Phone.Tasks;
namespace F5debugHowto79
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
CameraCaptureTask ccptureTask = new CameraCaptureTask();
ccptureTask.Completed += cameraTask_Completed;
ccptureTask.Show();
}
void cameraTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
ShareMediaTaskMethod(e.OriginalFileName);
}
}
void ShareMediaTaskMethod(string path)
{
ShareMediaTask smTask = new ShareMediaTask();
smTask.FilePath = path;
smTask.Show();
}
}
}
Now we are done with the code, just run the application by pressing F5 directly from the keyboard or we can use the Build and execute the project option from the tool bar to run the application. Once the Build is successful we can see the Windows Phone emulator with the application and the expected output.
No Comments
Berlinski is NOT a believer in "Intelligent Design," is he?