Ibraheem Osama Mohamed (ابراهيم اسامه محمد)

Welcome to my blog :)

Windows 8 Share Contract

leave a comment »

Is it easy to add a Share contract in Windows 8 ? There is nothing more simple. This post will help you to implement a Share contract for a text and an image in your application

1)Open Visual Studio -> New Metro black Application.
2)Open the MainPage.xaml -> add an Image and a TextBox
3)Open the MainPage.xaml.cs Add the following references

using Windows.ApplicationModel.DataTransfer;

using System.Text;

using Windows.Storage.Streams;

4)In the OnNavigatedTo event add the following code

protected override void OnNavigatedTo(NavigationEventArgs e)

{

     DataTransferManager.GetForCurrentView().DataRequested += OnDataRequested;

}

5)Now you need to add a method for OnDataRequested

void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)

        {

            //This is the code for sharing a Text

            var request = args.Request;         

            request.Data.Properties.Title = textbox1.Text;//or any text you want to share

            request.Data.Properties.Description = “This is the text of the textbox I’d like to share with you”;           

            var body = “Now you can make search contract as you like you can add many thing and make any application that you are using to search to use this text or image”;         

            request.Data.SetText(body);

 

            //This is the code for sharing an Image

            // var reference = RandomAccessStreamReference.CreateFromUri(image1.BaseUri);

            var reference = RandomAccessStreamReference.CreateFromUri(newUri(newUri(“ms-appx:///”), “Assets/LightGray.png”));

            request.Data.Properties.Thumbnail = reference;

            request.Data.SetBitmap(reference);

 

        }

6)And to make your application execute safely remove the OnDataRequested event when closing this application or moving to another application. This code make this mission

protected override void OnNavigatedFrom(NavigationEventArgs e)

        {           

            DataTransferManager.GetForCurrentView().DataRequested -= OnDataRequested;       

            base.OnNavigatedFrom(e);

        }

7)Run The application and you’ll get the result.

Ibraheem Osama Mohamed
@IbraheemOsamaMo

Advertisements

Written by Ibraheem Osama Mohamed

July 22, 2012 at 5:03 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: