Integrate packaged apps with Windows Share

This article explains how to integrate packaged apps with the Windows Share feature. Apps that are packaged with MSIX have package identity and are ready to register as a Share Target. The Share feature allows users to share content from one app to another. A packaged app will register as a Share Target in order to receive and handle shared files within the app.

What is Share Target?

Share Target is a feature that was introduced in Windows 8, and it allows an app to receive data from another app. Share Target works like a Clipboard but with dynamic content.

For the default share target registration to work with Win32 apps, the app needs to have a package identity and also handle the share arguments as ShareTargetActivatedEventArgs, which is a live object from the source app. It isn't a static memory content that is sent to the target app.

Note

In a C++ app, use the GetCurrentPackageFullName API to check if the running app has package identity. The API returns the APPMODEL_ERROR_NO_PACKAGE error code if it isn't running with package identity.

Prerequisites

To support ShareTargetActivatedEventArgs, the app must target Windows 10, version 2004 (build 10.0.19041.0) or later. This is the minimum target version for the feature.

Register as a Share Target

There are two steps required to implement the Share contract in your app.

Add a share target extension to appxmanifest

In Visual Studio's Solution Explorer, open the package.appxmanifest file of the Packaging project in your solution and add the share target extension.

<Extensions>
      <uap:Extension
          Category="windows.shareTarget">
        <uap:ShareTarget>
          <uap:SupportedFileTypes>
            <uap:SupportsAnyFileType />
          </uap:SupportedFileTypes>
          <uap:DataFormat>Bitmap</uap:DataFormat>
        </uap:ShareTarget>
      </uap:Extension>
</Extensions>

Add the supported data format that is supported by your application to the DataFormat configuration. In this case, the app supports sharing images, so the DataFormat is set to Bitmap.

Fetch Share Event arguments

Starting in Windows 10, version 1809, packaged apps can call the AppInstance.GetActivatedEventArgs method to retrieve certain kinds of app activation info during startup. For example, you can call this method to get information about app activation; whether it was triggered by opening a file, clicking an interactive toast, or using a registered protocol.

However, ShareTargetActivatedEventArgs activation info is supported only on Windows 10, version 2004, and later. So, the application should target to devices with this specific minimum version.

To see a Windows App SDK implementation, see the OnLaunched method in the Share Target sample app.

For other packaged apps, in the Main method of the application, check for AppInstance.GetActivatedEventArgs.

public static void Main(string[] cmdArgs)
{
    ...
    if (isRunningWithIdentity())
    {
        var activationArgs = AppInstance.GetActivatedEventArgs();
        if (activationArgs != null)
        {
            switch (activationArgs.Kind)
            {
                case ActivationKind.Launch:
                    HandleLaunch(activationArgs as LaunchActivatedEventArgs);
                    break;
                case ActivationKind.ToastNotification:
                    HandleToastNotification(activationArgs as ToastNotificationActivatedEventArgs);                                     
                    break;
                case ActivationKind.ShareTarget:
                    HandleShareAsync(activationArgs as ShareTargetActivatedEventArgs);
                    break;
                default:
                    HandleLaunch(null);
                    break;
            }
        }
    }
}

See the Photo Store Demo app for a complete implementation.

Handle shared files

The following code snippet shows how to handle shared files in a packaged app. The code snippet is part of the HandleShareAsync method that is called when the app is activated as a Share Target in the previous example.

static async void HandleShareAsync(ShareTargetActivatedEventArgs args)
{
    ShareOperation shareOperation = args.ShareOperation;
    shareOperation.ReportStarted();

    if (shareOperation.Data.Contains( 
        Windows.ApplicationModel.DataTransfer.StandardDataFormats.StorageItems))
    {
        try
        {
            IReadOnlyList<IStorageItem> items = await shareOperation.Data.GetStorageItemsAsync();
            var file = (IStorageFile)items[0]; 
            string path = file.Path;
            var image = new ImageFile(path);
            image.AddToCache();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    shareOperation.ReportCompleted();

    // app launch code
}

See also