共用方式為


Sharing and exchanging data (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]

Here we describe the various ways that apps can share and exchange data.

Users today expect to be able to use technology to connect and share with other people. Whether a user wants to share a link with their social network, copy a picture into a report, or upload a file to cloud storage, apps today need to ensure that the data they use is also available for users to share and exchange.

There are several ways for apps to exchange data with other apps. One of these ways, the Share contract, is new and requires some explanation so you can use it effectively. Another way, the Clipboard, is something that most developers (regardless of what platform you write code for) are familiar with. Still, to use the Clipboard today you need to consider not just your content, but also the UI for copying and pasting. This is especially true if your app uses touch as a primary way to get user input. And still another way, saving, is something that's also been around for some time, but hasn't been used in this way before.

We've put together some info on these different ways to share and exchange data to help you decide what options make the most sense for your app and for your users. To see these options in action firsthand, feel free to jump over to our code gallery and check out the following samples:

If you already know what method you want to use and just need a hand getting started, these quickstart topics should help you out:

Share contract

The Share contract is one way users can quickly exchange data between apps. For example, a user might want to share a webpage with their friends using a social networking app, or save a link in a notes app to refer to later. Consider using a Share contract if your app receives content in scenarios that a user can quickly complete while in the context of another app. Common content in one of these scenarios might be a webpage or a group of photos.

Note  

The Share charm is available at all times to the user, and provides quick and easy access to all apps that can receive shared content.

Note  

The Charms bar does not exist on Windows Phone 8.1, so you must include the share option programmatically. It can either be accessible at all times as part of an app bar, or be associated with a specific control on a particular page.

There are two sides to a share contract: the source, which shares the data, and the target, which receives it. To learn more about adding share to your app, we recommend that you start with Adding share. From there, you'll learn more about how share works, and how to get started. You can also check out two full code samples, the Sharing content source app sample and the Sharing content target app sample.

To learn more about participating in the Share contract, see Receiving shared content.

Tap and send

Another sharing feature is called Tap and send. The Tap and send option is included automatically in apps that act as Share sources. It uses Near Field Communication (NFC) to share data between app users. In particular, Tap and send is ideal for sharing links and files with a nearby friend. Tap and send is available only on devices that support NFC and proximity technology.

Note  

Tap and send automatically appears on the Devices charm when an app has something to share, so the user can easily access it using a simple touch gesture or mouse click.

Note  

The Charms bar does not exist on Windows Phone 8.1, so you must include the Tap and send option programmatically. It can either be accessible at all times as part of an app bar, or be associated with a specific control on a particular page.

Learn more about about proximity and tapping.

Copy and Paste

Copy-and-paste is the classic way to exchange data either between apps, or within an app. Just about every app can support copy-and-paste operations to some degree, and it's a familiar set of actions that users have come to rely on as part of their online experience. Initially, copy-and-paste actions seem very similar to Share, so it's tempting to just use one or the other. However, good developers recognize that these are two different features, and can provide very different user experiences.

Note  

One advantage the Clipboard has over sharing is that it's the only way to move data from Windows Store apps to the Desktop, and vice versa. You should also consider how the user can access the copy-and-paste commands. In a mouse and keyboard environment, the classic shortcut keys are probably sufficient. If the user is working on a tablet, however, those shortcuts aren't available. Instead, consider using the app bar, a context-sensitive menu, or a different control to display copy-and-paste commands. To learn more, have a look at our Guidelines and checklist for Clipboard commands or the full Clipboard app code sample.

Receiving files through save

Choosing to let other apps save files to your app is another way for a user to exchange data between two apps. For example, say you're working on a proposal using Microsoft Word and you want to make it available to your coworkers. When you save the file, you can pick Microsoft OneDrive as the save location for the file. You don't need to keep track of your file because, by saving it to OneDrive, you've informed OneDrive that it should keep track of it for you.

Receiving files that are saved to your app requires that your app participate in the File Save Picker contract. Apps that participate in this contract are hosted in the file picker that is available from apps that provide an entry point for saving files. Consider participating in the File Save Picker contract if users expect your app to be a typical location for saving a file, and they often want to specify a file name, file type, or location for the file. If you're already participating in the File Open Picker contract, adding the File Save Picker contract is a natural extension to what users already expect from your app. To learn more about participating the File Save Picker contract, see Quickstart: Accessing files with file pickers or check out the full code sample.

Which data sharing and exchanging options should you support?

Letting other apps use your app as a save location for files makes sense when the user is working with files and folders. It's less intuitive when it comes to smaller sets of data, such as a URL or a text selection. In those cases, the Share contract or the copy and paste commands are better options. That said, there are plenty of situations in which it makes sense to support both the Share charm and saving. For example, consider a user who wants to store a file using a cloud-storage service, such as OneDrive. In this situation, both the Share contract and saving are equally valid options. In the end, you need to consider how your user interacts with your app, and support the contracts and features that match user expectations and provide the best overall experience.