How to share text and links from a Direct3D app for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

This topic shows you how to give users the ability to share text and links from a Windows Phone Direct3D app. Windows Phone has a built-in Share UI you can use to give users the ability to share text and links with the contacts or social networks that they select. You launch the Share UI by calling the ShowShareUI()()() method. If you want the Share UI to be prepopulated with data, you need to hook up an event handler for the DataRequested event. This handler is called after you call ShowShareUI. You can get a DataPackage object from the handler’s event args and use it to set the text or link to be shared, as well as the description and title of the sharing task.

To share text or links from a managed app, see How to use the share link task for Windows Phone 8 and How to use the share status task for Windows Phone 8.

  1. Create a new Windows Phone Direct3D app. This example assumes you name the project NativeShareLauncher.

  2. Add the declaration for an event handler for the DataRequested event to the NativeShareLauncher.h header file.

        void DataRequestedEventHandler(Windows::ApplicationModel::DataTransfer::DataTransferManager^ sender, Windows::ApplicationModel::DataTransfer::DataRequestedEventArgs^ e);
  3. Add a using directive to the top of NativeShareLauncher.cpp to include the Windows.ApplicationModel.DataTransfer namespace.

    using namespace Windows::ApplicationModel::DataTransfer;
  4. Obtain an instance of the DataTransferManager class by calling the GetForCurrentView()()() factory method, and then assign the event handler for the DataRequested event. In this example, this code is added to the Activated event handler that is included in the Direct3D app project template and is called whenever the application view is activated.

    void NativeShareLauncher::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
        DataTransferManager^ dataTransferManager = DataTransferManager::GetForCurrentView();
        dataTransferManager->DataRequested += ref new TypedEventHandler<DataTransferManager^, DataRequestedEventArgs^>(this, &NativeShareLauncher::DataRequestedEventHandler);
  5. In the DataRequested event handler, set the title, description, main text, and URI on the DataPackage object from the event args. These properties values are all optional and will be displayed differently in the Share UI, depending on what type of sharing the user selects.

    void NativeShareLauncher::DataRequestedEventHandler(DataTransferManager^ sender, DataRequestedEventArgs^ e)
        e->Request->Data->Properties->Title = "Share Text Title";
        e->Request->Data->Properties->Description = "Share Text Description";
        e->Request->Data->SetText("Main text");    
        e->Request->Data->SetUri(ref new Uri(""));
  6. Call ShowShareUI()()(). In this example, the Share UI is launched from the OnPointerPressed method that is included in the Direct3D app project template, and is called when the user taps anywhere on the screen.