- Windows 10 开发教程
- Windows 10 - 家庭版
- Windows 10 - 简介
- Windows 10 – UWP
- Windows 10 – 第一个应用
- Windows 10 - 应用商店
- Windows 10 - XAML 控件
- Windows 10 - 数据绑定
- Windows 10 - XAML 性能
- Windows 10 - 自适应设计
- Windows 10 - 自适应 UI
- Windows 10 - 自适应代码
- Windows 10 - 文件管理
- Windows 10 - SQLite 数据库
- Windows 10 – 通信
- Windows 10 - 应用本地化
- Windows 10 - 应用生命周期
- Windows 10 - 后台执行
- Windows 10 - 应用服务
- Windows 10 - Web 平台
- Windows 10 - 连接体验
- Windows 10 - 导航
- Windows 10 - 网络
- Windows 10 - 云服务
- Windows 10 - 实时磁贴
- Windows 10 - 共享契约
- Windows 10 - 移植到 Windows
- Windows 10 有用资源
- Windows 10 - 快速指南
- Windows 10 - 有用资源
- Windows 10 - 讨论
Windows 10 开发 - 共享契约
在本章中,我们将学习如何在应用程序之间共享数据。用户经常遇到他们希望与他人共享或在其他应用程序中使用的信息。如今,用户希望利用技术与他人联系和共享。
用户可能希望共享 -
- 与他们的社交网络的链接
- 将图片复制到报告中
- 将文件上传到云存储
如今的应用程序需要确保它们使用的数据也可供用户共享和交换。共享是一个轻量级功能,易于添加到您的 UWP 应用程序中。应用程序可以通过多种方式与其他应用程序交换数据。
在 UWP 应用程序中,共享功能可以通过以下方式支持;
首先,应用程序可以是源应用程序,提供用户想要共享的内容。
其次,应用程序可以是目标应用程序,用户将其选择为共享内容的目标。
一个应用程序也可以同时是源应用程序和目标应用程序。
共享内容
从作为源应用程序的应用程序共享内容非常简单。要执行任何共享操作,您将需要 **DataPackage** 类对象。此对象包含用户想要共享的数据。
**DataPackage** 对象可以包含以下类型的内容 -
- 纯文本
- 统一资源标识符 (URI)
- HTML
- 格式化文本
- 位图
- 文件
- 开发者定义的数据
共享数据时,您可以包含上述一种或多种格式。要在您的应用程序中支持共享,您首先需要获取 **DataTransferManager** 类的实例。
然后,它将注册一个事件处理程序,该处理程序在每次发生 **DataRequested** 事件时调用。
DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView(); dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.ShareTextHandler);
当您的应用程序收到 **DataRequest** 对象时,您的应用程序就可以添加用户想要共享的内容。
private void ShareTextHandler(DataTransferManager sender, DataRequestedEventArgs e){
DataRequest request = e.Request;
// The Title is mandatory
request.Data.Properties.Title = "Share Text Example";
request.Data.Properties.Description = "A demonstration that shows how to share text.";
request.Data.SetText("Hello World!");
}
您的应用程序共享的任何内容都必须包含两个属性 -
- 一个标题属性,它是必需的并且必须设置。
- 内容本身。
接收共享内容
如果您希望您的应用程序能够接收共享内容,那么您需要做的第一件事是声明它支持 **共享契约**。声明后,系统将允许您的应用程序接收内容。
添加对共享契约的支持 -
双击 **package.appmanifest** 文件。
转到 **声明** 选项卡。从 **可用声明** 列表中选择 **共享目标**,然后单击 **添加** 按钮。
如果您希望您的应用程序接收任何类型的文件作为共享内容,则可以指定文件类型和数据格式。
要指定您支持的数据格式,请转到 **声明** 页面的 **数据格式** 部分,然后单击 **添加新项**。
键入您支持的数据格式的名称。例如,**“文本”**。
要指定您支持的文件类型,请在 **声明** 页面的 **支持的文件类型** 部分中,单击 **添加新项**。
键入您要支持的文件名扩展名,例如 **.pdf**
如果您要支持 **所有文件** 类型,请选中 **支持任何文件类型** 复选框。
当用户选择您的应用程序作为共享数据的目标应用程序时,将触发 **OnShareTargetActivated** 事件。
您的应用程序需要处理此事件以处理用户想要共享的数据。
protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args) {
// Code to handle activation goes here.
}
用户想要与任何应用程序共享的所有数据都包含在 **ShareOperation** 对象中。您还可以检查它包含的数据的格式。
下面是处理纯文本格式的 **共享内容** 的代码片段。
ShareOperation shareOperation = args.ShareOperation;
if (shareOperation.Data.Contains(StandardDataFormats.Text)) {
string text = await shareOperation.Data.GetTextAsync();
// To output the text from this example, you need a TextBlock control
// with a name of "sharedContent".
sharedContent.Text = "Text: " + text;
}
让我们通过创建一个新的 UWP 项目来查看一个简单的示例,该项目将共享一个网页链接。
下面是在其中创建了一个带有某些属性的按钮的 XAML 代码。
<Page
x:Class = "UWPSharingDemo.MainPage"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local = "using:UWPSharingDemo"
xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable = "d">
<Grid Background = "{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel Orientation = "Vertical">
<TextBlock Text = "Share Web Link" Style = "{StaticResource
HeaderTextBlockStyle}" Margin = "30"></TextBlock>
<Button Content = "Invoke share contract" Margin = "10"
Name = "InvokeShareContractButton" Click = "InvokeShareContractButton_Click"
></Button>
</StackPanel>
</Grid>
</Page>
下面是实现了按钮单击事件的 C# 代码,以及 URI 共享代码。
using System;
using Windows.ApplicationModel.DataTransfer;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
// The Blank Page item template is documented at
http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace UWPSharingDemo {
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page {
DataTransferManager dataTransferManager;
public MainPage() {
this.InitializeComponent();
dataTransferManager = DataTransferManager.GetForCurrentView();
dataTransferManager.DataRequested += dataTransferManager_DataRequested;
}
void dataTransferManager_DataRequested(DataTransferManager sender,
DataRequestedEventArgs args) {
Uri sharedWebLink = new Uri("https://msdn.microsoft.com");
if (sharedWebLink != null) {
DataPackage dataPackage = args.Request.Data;
dataPackage.Properties.Title = "Sharing MSDN link";
dataPackage.Properties.Description = "The Microsoft Developer Network (MSDN)
is designed to help developers write applications using Microsoft
products and technologies.";
dataPackage.SetWebLink(sharedWebLink);
}
}
private void InvokeShareContractButton_Click(object sender, RoutedEventArgs e) {
DataTransferManager.ShowShareUI();
}
}
}
编译并执行上述代码后,您将在模拟器上看到以下页面。
单击按钮后,它将提供在哪个应用程序上共享的选项。
单击消息,将显示以下窗口,您可以从中将链接发送给任何人。