Windows 10开发 - 共享合同

在本章中,我们将学习如何在应用程序之间共享数据.用户经常会遇到他们很高兴与某人分享或在其他应用程序中使用它的信息.如今,用户希望使用技术与其他人联系和分享.

用户可能想要分享&减去;

  • 与其社交网络的链接

  • 将图片复制到报告中

  • 将文件上传到云端存储

今天的应用程序,需要确保他们使用的数据也可供用户共享和交换.共享是一种轻量级功能,可以轻松添加到您的UWP应用程序中.应用有多种方式可以与其他应用交换数据.

在UWP应用中,可以通过以下方式支持共享功能;

  • 首先,应用程序可以是提供用户想要共享的内容的源应用程序.

  • 其次,该应用可以是用户选择作为共享内容目标的目标应用.

  • 应用也可以是源应用和目标应用.

分享内容

从应用程序共享内容,这是一个来源应用程序非常简单.要执行任何共享操作,您需要 DataPackage 类对象.此对象包含用户想要共享的数据.

以下类型的内容可以包含在 DataPackage 对象 :

$ b中$ b

  • 纯文本

  • 统一资源标识符(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!");
}

您的应用程序共享的任何内容必须包含两个属性 :

  • 标题属性,必须设置.

  • 内容本身.

接收共享内容

如果您希望您的应用程序可以接收共享内容,那么您需要做的第一件事就是声明它支持共享内容.声明后,系统将允许您的应用程序接收内容.

添加对Share契约的支持 :

  • 双击 package.appmanifest 文件.

  • 转到声明标签.从可用声明列表中选择共享目标,然后单击添加按钮.

分享目标

  • 如果您希望应用程序接收任何类型的文件作为共享内容,则可以指定文件类型和数据格式.

  • 指定您支持的数据格式转到声明页面的数据格式部分,然后点击添加新.

  • 键入您支持的数据格式的名称.例如,"文本".

  • 要指定您支持的文件类型,请在支持的文件类型 声明页面的部分,点击添加新.

  • 输入文件扩展名您想要支持的内容,例如 .pdf

  • 如果您想支持所有文件类型,检查 SupportsAnyFileType 框.

支持所有文件

  • 当用户选择您的应用程序作为共享数据的目标应用程序时, 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(); 
      }
		
   } 
}

编译并执行上述代码时,您将看到模拟器上的以下页面.

编译并执行

当按钮时单击,它将提供共享在哪个应用程序上的选项.

分享申请

点击短信,将显示以下窗口从哪里可以发送给任何人的链接.

接收应用程序