Sdílet prostřednictvím


How to create a QuickLink (XAML)

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

Users want to share content with friends and family as an extension of their computer experience. Apps that embrace this concept create an environment that enables users to share content quickly and effortlessly. You can create an elegant sharing experience by using a custom shortcut called a QuickLink. A QuickLink acts as a link to your app that's customized for a specific set of user actions.

When users share content, they typically want to do more than just broadcast what they're sharing. They want to make choices about how they share their content and who they share that content with. A common example of this is with email. If a user wants to share something through email, they have to supply the email addresses of the people they want to share with. And, while a user might not mind supplying these email addresses once, adding them repeatedly becomes tedious. A better experience is for the email app to create a QuickLink. Then the user can repeat their choices in one step.

Before we show you how your app can create QuickLinks, there are a few things you should know. First, a QuickLink doesn't actually store any data. Instead, it contains an identifier that, when selected, is sent to your app. For QuickLinks to work, your app needs to store the data somewhere—such as in the cloud, or on the user's computer—along with its associated ID. There are lots of ways you can do this—for one way of storing data, see Managing application data.

Second, you should read the topic, QuickStart: Receiving shared content. That topic describes the basic steps your app needs to follow when users select it to share content.

What you need to know

Technologies

Prerequisites

  • You should be familiar with Visual Studio and its associated templates.
  • You should be familiar with C# development.
  • You should know how to receive shared content, as described in QuickStart: Receiving shared content.

Instructions

Step 1: Add the necessary namespaces

To create QuickLinks, you'll need the Windows.ApplicationModel.DataTransfer and Windows.ApplicationModel.DataTransfer.ShareTarget namespaces:

using Windows.ApplicationModel.DataTransfer;
using Windows.ApplicationModel.DataTransfer.ShareTarget;

You need to configure the QuickLink with two types of information. The first is information that the system needs in order to display and process the QuickLink correctly. The most important information is an ID that the system can return to your app when the user selects the QuickLink, and a list of file formats that the QuickLink supports. The system shows the QuickLink only when the DataPackage contains one of these file types.

The second type of information helps the user identify the QuickLink and its purpose. Typically, this information includes the title of the QuickLink and an icon that represents your app. We recommend that you use the same icon that users would see if they were selecting your app without a QuickLink. That way, the user knows that the link is associated with your app.

QuickLink quickLink = new QuickLink
{
    // The ID and Title come from text that the user enters into TextBox controls.
    Id = quickLinkId.Text,
    Title = quickLinkTitle.Text,
    SupportedFileTypes = { "*" },
    SupportedDataFormats = { StandardDataFormats.Text, StandardDataFormats.Uri, 
        StandardDataFormats.Bitmap, StandardDataFormats.StorageItems, dataFormatName }
};

StorageFile iconFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.CreateFileAsync("images\\user.png", CreationCollisionOption.OpenIfExists);
quickLink.Thumbnail = RandomAccessStreamReference.CreateFromFile(iconFile);

When you've finished creating the QuickLink, you can send it to the system by calling the ShareOperation.ReportCompleted method.

this.shareOperation.ReportCompleted(quickLink);

Complete example

The following function creates and returns a QuickLink. Call this function after your app finishes a share operation.

async void reportCompleted(object sender, RoutedEventArgs e)
{
    QuickLink quickLinkInfo = new QuickLink
    {
        // The ID and Title come from text that the user enters into TextBox controls.
        Id = quickLinkId.Text,
        Title = quickLinkTitle.Text,

        SupportedFileTypes = { "*" },
        SupportedDataFormats = { StandardDataFormats.Text, StandardDataFormats.Uri, 
            StandardDataFormats.Bitmap, StandardDataFormats.StorageItems, dataFormatName }
    };

    StorageFile iconFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.CreateFileAsync("images\\user.png", CreationCollisionOption.OpenIfExists);
    quickLinkInfo.Thumbnail = RandomAccessStreamReference.CreateFromFile(iconFile);
    this.shareOperation.ReportCompleted(quickLinkInfo);
}

Sharing content target app sample

Sharing and exchanging data

Quickstart: Receiving shared content

QuickLink

ShareOperation

Windows.ApplicationModel.DataTransfer

Windows.ApplicationModel.DataTransfer.Share

Guidelines for debugging target apps