背水一战 Windows 10 (100) - 应用间通信: 分享

时间:2023-03-09 06:07:01
背水一战 Windows 10 (100) - 应用间通信: 分享

[源码下载]

背水一战 Windows 10 (100) - 应用间通信: 分享

作者:webabcd

介绍
背水一战 Windows 10 之 应用间通信

  • 分享

示例
1、本例用于演示如何开发一个分享的分享源
App2AppCommunication/ShareSource.xaml

<Page
x:Class="Windows10.App2AppCommunication.ShareSource"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.App2AppCommunication"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" /> <Button Content="Share Text" Click="Button_Click" Margin="5" /> <Button Content="Share Web Link" Click="Button_Click" Margin="5" /> <Button Content="Share Application Link" Click="Button_Click" Margin="5" /> <Button Content="Share Image" Click="Button_Click" Margin="5" /> <Button Content="Share File" Click="Button_Click" Margin="5" /> <Button Content="Share Html" Click="Button_Click" Margin="5" /> <Button Content="Share Custom Protocol" Click="Button_Click" Margin="5" /> <Button Content="Share With Deferral" Click="Button_Click" Margin="5" /> <Button Content="Share Failed" Click="Button_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>

App2AppCommunication/ShareSource.xaml.cs

/*
* 本例用于演示如何开发一个分享的分享源(分享目标的示例参见 /MyShareTarget/MainPage.xaml.cs)
*
* 分享源 - 提供分享数据的 app
* 分享目标 - 接收并处理分享数据的 app
* 分享面板 - 点击“分享”后出来的,包含了一堆分享目标的面板
*
*
* DataTransferManager - 分享数据管理器
* GetForCurrentView() - 返回当前窗口关联的 DataTransferManager 对象
* ShowShareUI() - 弹出分享面板,以开始分享操作
* DataRequested - 分享操作开始时(即弹出分享面板后)所触发的事件,事件参数为 DataTransferManager 和 DataRequestedEventArgs
* TargetApplicationChosen - 选中了分享面板上的某个分享目标时所触发的事件,事件参数为 DataTransferManager 和 TargetApplicationChosenEventArgs
*
* TargetApplicationChosenEventArgs - TargetApplicationChosen 的事件参数
* ApplicationName - 选中的分享目标的名称
*
* DataRequestedEventArgs - DataRequested 的事件参数
* Request - 返回 DataRequest 类型的数据
*
* DataRequest - 一个对象,其包括了分享的内容和错误提示
* FailWithDisplayText() - 当需要分享的数据不合法时,需要在分享面板上显示的提示信息
* Data - 需要分享的内容,返回 DataPackage 对象
*
* DataPackage - 分享内容(注:复制到剪切板的内容也是通过此对象来封装)
* Properties - 返回 DataPackagePropertySetView 对象
* Properties.Title - 分享数据的标题
* Properties.Description - 分享数据的描述
* Properties.Thumbnail - 分享数据的缩略图
* ApplicationName - 分享源的 app 的名称
* PackageFamilyName - 分享源的 app 的包名
* ApplicationListingUri - 分享源的 app 在商店中的地址
* ContentSourceApplicationLink - 内容源的 ApplicationLink
* ContentSourceWebLink - 内容源的 WebLink
* Square30x30Logo - 分享源的 app 的 30 x 30 的 logo
* LogoBackgroundColor - 分享源的 app 的 logo 的背景色
* FileTypes - 获取分享数据中包含的文件类型
* SetText(), SetWebLink(), SetApplicationLink(), SetHtmlFormat(), SetBitmap(), SetStorageItems(), SetData(), SetDataProvider() - 设置需要分享的各种格式的数据,详细用法见下面的相关 demo(注:一个 DataPackage 可以有多种不同格式的数据)
* ResourceMap - IDictionary<string, RandomAccessStreamReference> 类型,分享 html 时如果其中包含了本地资源的引用(如引用了本地图片),则需要通过 ResourceMap 传递
* GetView() - 返回 DataPackageView 对象,其相当于 DataPackage 的一个只读副本,详细说明见 /MyShareTarget/MainPage.xaml.cs
*
*
* 异步分享:
* 1、DataPackage 通过 SetDataProvider() 传递数据,其对应的委托的参数为一个 DataProviderRequest 类型的数据
* 2、DataProviderRequest.GetDeferral() 用于获取 DataProviderDeferral 对象以开始异步处理,然后通过 DataProviderDeferral.Complete() 完成异步操作
*
*
* 注:
* 一个 DataPackage 可以包含多种不同格式的数据(本例为了演示的清晰,每次只会分享一种格式的数据)
*/ using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using System.Linq;
using Windows.Graphics.Imaging;
using Windows.UI; namespace Windows10.App2AppCommunication
{
public sealed partial class ShareSource : Page
{
// 当前需要分享的内容的类型
private string _shareType = "Share Text";
// 需要分享的文件集合
private IReadOnlyList<StorageFile> _selectedFiles; public ShareSource()
{
this.InitializeComponent();
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 初始化 DataTransferManager
DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
dataTransferManager.DataRequested += dataTransferManager_DataRequested;
dataTransferManager.TargetApplicationChosen += dataTransferManager_TargetApplicationChosen;
} // 分享操作开始时(即弹出分享面板后)
void dataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
// 根据需要分享的内容的类型执行指定的方法
switch (_shareType)
{
case "Share Text":
ShareText(sender, args);
break;
case "Share Web Link":
ShareWebLink(sender, args);
break;
case "Share Application Link":
ShareApplicationLink(sender, args);
break;
case "Share Image":
ShareImage(sender, args);
break;
case "Share File":
ShareFile(sender, args);
break;
case "Share With Deferral":
ShareWithDeferral(sender, args);
break;
case "Share Html":
ShareHtml(sender, args);
break;
case "Share Custom Protocol":
ShareCustomProtocol(sender, args);
break;
case "Share Failed":
ShareFailed(sender, args);
break;
default:
break;
}
} // 选中了分享面板上的某个分享目标时
void dataTransferManager_TargetApplicationChosen(DataTransferManager sender, TargetApplicationChosenEventArgs args)
{
// 显示用户需要与其分享内容的应用程序的名称
lblMsg.Text = "分享给:" + args.ApplicationName;
} private async void Button_Click(object sender, RoutedEventArgs e)
{
_shareType = (sender as Button).Content.ToString(); // 如果需要分享文件,则提示用户选择文件
if (_shareType == "Share Image" || _shareType == "Share File" || _shareType == "Share With Deferral")
{
FileOpenPicker filePicker = new FileOpenPicker
{
ViewMode = PickerViewMode.List,
SuggestedStartLocation = PickerLocationId.PicturesLibrary,
FileTypeFilter = { "*" }
// FileTypeFilter = { ".jpg", ".png", ".bmp", ".gif", ".tif" }
}; _selectedFiles = await filePicker.PickMultipleFilesAsync();
if (_selectedFiles.Count > )
{
// 弹出分享面板,以开始分享操作
DataTransferManager.ShowShareUI();
}
}
else
{
// 弹出分享面板,以开始分享操作
DataTransferManager.ShowShareUI();
}
} // 分享文本的 Demo
private void ShareText(DataTransferManager dtm, DataRequestedEventArgs args)
{
DataPackage dataPackage = GetDataPackage(args);
dataPackage.SetText("需要分享的详细内容");
} // 分享 WebLink 的 Demo
private void ShareWebLink(DataTransferManager dtm, DataRequestedEventArgs args)
{
DataPackage dataPackage = GetDataPackage(args);
dataPackage.SetWebLink(new Uri("http://webabcd.cnblogs.com"));
} // 分享 ApplicationLink 的 Demo
private void ShareApplicationLink(DataTransferManager dtm, DataRequestedEventArgs args)
{
DataPackage dataPackage = GetDataPackage(args);
dataPackage.SetApplicationLink(new Uri("webabcd:data"));
} // 分享图片的 Demo(关于如何为分享的图片减肥请参见本例的“异步分享的 Demo”)
private void ShareImage(DataTransferManager dtm, DataRequestedEventArgs args)
{
DataPackage dataPackage = GetDataPackage(args); // 分享选中的所有文件中的第一个文件(假设其是图片)
RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromFile(_selectedFiles.First());
dataPackage.Properties.Thumbnail = imageStreamRef;
dataPackage.SetBitmap(imageStreamRef);
} // 分享文件的 Demo
private void ShareFile(DataTransferManager dtm, DataRequestedEventArgs args)
{
DataPackage dataPackage = GetDataPackage(args);
dataPackage.SetStorageItems(_selectedFiles);
} // 分享 html 的 Demo
private void ShareHtml(DataTransferManager dtm, DataRequestedEventArgs args)
{
string localImage = "ms-appx:///Assets/StoreLogo.png";
string htmlExample = "<p><b>webabcd</b><img src=\"" + localImage + "\" /></p>";
// 为 html 添加分享所需的必要的标头,以保证可以正常进行 html 的分享操作
string htmlFormat = HtmlFormatHelper.CreateHtmlFormat(htmlExample); DataPackage dataPackage = GetDataPackage(args);
dataPackage.SetHtmlFormat(htmlFormat); // 设置本地图像数据(如果需要分享的 html 包含本地图像,则只能通过这种方法分享之)
RandomAccessStreamReference streamRef = RandomAccessStreamReference.CreateFromUri(new Uri(localImage));
dataPackage.ResourceMap[localImage] = streamRef; /*
* 以下演示如何分享 WebView 中的被用户选中的 html
* 具体可参见:Controls/WebViewDemo/WebViewDemo6.xaml
*
DataPackage dataPackage = WebView.DataTransferPackage;
DataPackageView dataPackageView = dataPackage.GetView(); if ((dataPackageView != null) && (dataPackageView.AvailableFormats.Count > 0))
{
dataPackage.Properties.Title = "Title";
dataPackage.Properties.Description = "Description"; args.Request.Data = dataPackage;
}
*/
} // 分享自定义协议的 Demo
private void ShareCustomProtocol(DataTransferManager dtm, DataRequestedEventArgs args)
{
DataPackage dataPackage = GetDataPackage(args); // 指定需要分享的自定义协议和自定义数据,第一个参数是自定义数据的格式 id,也就是自定义协议(要使用标准格式 id 的话,就是 Windows.ApplicationModel.DataTransfer.StandardDataFormats 枚举)
// 需要在 SourceTarget 的 Package.appxmanifest 中的“共享目标”声明中对自定义格式 id 做相应的配置
dataPackage.SetData("http://webabcd/sharedemo", "自定义数据");
} // 异步分享的 Demo(在分享内容需要较长时间才能计算出来的场景下,应该使用异步分享)
private void ShareWithDeferral(DataTransferManager dtm, DataRequestedEventArgs args)
{
DataPackage dataPackage = GetDataPackage(args); dataPackage.Properties.Thumbnail = RandomAccessStreamReference.CreateFromFile(_selectedFiles.First());
// 通过委托来提供分享数据,当用户点击了分享目标后会调用此委托,即不马上提供分享数据,而是等到用户点击了分享目标后再异步准备数据
dataPackage.SetDataProvider(StandardDataFormats.Bitmap, providerRequest => this.OnDeferredImageRequestedHandler(providerRequest, _selectedFiles.First()));
} // 用户点击了分享目标后会调用此方法
private async void OnDeferredImageRequestedHandler(DataProviderRequest providerRequest, StorageFile imageFile)
{
// 获取 DataProviderDeferral,以开始异步处理
DataProviderDeferral deferral = providerRequest.GetDeferral();
InMemoryRandomAccessStream inMemoryStream = new InMemoryRandomAccessStream(); try
{
// 将用户选中的图片缩小一倍,然后再分享
IRandomAccessStream imageStream = await imageFile.OpenAsync(FileAccessMode.Read);
BitmapDecoder imageDecoder = await BitmapDecoder.CreateAsync(imageStream);
BitmapEncoder imageEncoder = await BitmapEncoder.CreateForTranscodingAsync(inMemoryStream, imageDecoder);
imageEncoder.BitmapTransform.ScaledWidth = (uint)(imageDecoder.OrientedPixelWidth * 0.5);
imageEncoder.BitmapTransform.ScaledHeight = (uint)(imageDecoder.OrientedPixelHeight * 0.5);
await imageEncoder.FlushAsync(); // 停 3 秒,以模拟长时间任务
await Task.Delay(); providerRequest.SetData(RandomAccessStreamReference.CreateFromStream(inMemoryStream));
}
finally
{
// 完成异步操作
deferral.Complete();
}
} // 需要分享的数据不合法时如何处理的 Demo
private void ShareFailed(DataTransferManager dtm, DataRequestedEventArgs args)
{
// 判断需要分享的数据是否合法,不合法的话就调用下面的方法,用以在分享面板上显示指定的提示信息
if (true)
{
args.Request.FailWithDisplayText("需要分享的数据不合法,分享操作失败");
}
} // 用于设置分享的数据
private DataPackage GetDataPackage(DataRequestedEventArgs args)
{
DataPackage dataPackage = args.Request.Data; dataPackage.Properties.Title = "Title";
dataPackage.Properties.Description = "Description";
dataPackage.Properties.ContentSourceApplicationLink = new Uri("webabcd:data");
dataPackage.Properties.ContentSourceWebLink = new Uri("http://webabcd.cnblogs.com/"); // 下面这些数据不指定的话,系统会自动为其设置好相应的值
dataPackage.Properties.ApplicationListingUri = new Uri("https://www.microsoft.com/store/apps/9pd68q7017mw");
dataPackage.Properties.ApplicationName = "ApplicationName";
dataPackage.Properties.PackageFamilyName = "PackageFamilyName";
dataPackage.Properties.Square30x30Logo = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/StoreLogo.png"));
dataPackage.Properties.LogoBackgroundColor = Colors.Red; return dataPackage;
}
}
}

2、本例用于演示如何开发一个分享的分享目标
App.xaml.cs

        // 通过“分享”激活应用程序时所调用的方法
protected override void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
var rootFrame = new Frame();
rootFrame.Navigate(typeof(MainPage), args);
Window.Current.Content = rootFrame; Window.Current.Activate();
}

MyShareTarget/MainPage.xaml

<Page
x:Class="MyShareTarget.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyShareTarget"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="{ThemeResource SystemAccentColor}"> <ScrollViewer Name="scrollView" Margin="10 0 10 10">
<StackPanel>
<TextBlock Name="lblMsg" Margin="5" /> <StackPanel Margin="5">
<TextBlock Text="Square30x30Logo: " />
<Image Name="imgLogo" HorizontalAlignment="Left" Width="100" Height="100" />
</StackPanel> <StackPanel Margin="5">
<TextBlock Text="分享图片的缩略图: " />
<Image Name="imgThumbnail" HorizontalAlignment="Left" Width="300" />
</StackPanel> <StackPanel Margin="5">
<TextBlock Text="分享图片的原图: " />
<Image Name="imgBitmap" HorizontalAlignment="Left" Width="300" />
</StackPanel>
</StackPanel>
</ScrollViewer> </Grid>
</Page>

MyShareTarget/MainPage.xaml.cs

/*
* 本例用于演示如何开发一个分享的分享目标(分享源的示例参见 /Windows10/App2AppCommunication/ShareSource.xaml.cs)
* 1、在 Package.appxmanifest 中新增一个“共享目标”声明,并做相关配置(支持的文件类型,支持的分享数据的格式),类似如下
* <Extensions>
* <uap:Extension Category="windows.shareTarget">
* <uap:ShareTarget>
* <uap:SupportedFileTypes>
* <uap:SupportsAnyFileType />
* </uap:SupportedFileTypes>
* <uap:DataFormat>Text</uap:DataFormat>
* <uap:DataFormat>WebLink</uap:DataFormat>
* <uap:DataFormat>ApplicationLink</uap:DataFormat>
* <uap:DataFormat>Html</uap:DataFormat>
* <uap:DataFormat>Bitmap</uap:DataFormat>
* <uap:DataFormat>http://webabcd/sharedemo</uap:DataFormat>
* </uap:ShareTarget>
* </uap:Extension>
* </Extensions>
* 2、在 App.xaml.cs 中 override void OnShareTargetActivated(ShareTargetActivatedEventArgs args),以获取相关的分享信息
*
*
* ShareTargetActivatedEventArgs - 当 app 由分享激活时的事件参数
* ShareOperation - 返回一个 ShareOperation 类型的对象
* PreviousExecutionState - 此 app 被分享激活前的执行状态(ApplicationExecutionState 枚举:NotRunning, Running, Suspended, Terminated, ClosedByUser)
* SplashScreen - 启动画面对象
*
* ShareOperation - 分享操作的相关信息
* Data - 返回 DataPackageView 对象,其相当于 DataPackage 的一个只读副本
* ReportCompleted(), ReportStarted(), ReportDataRetrieved(), ReportError(), ReportSubmittedBackgroundTask() - 顾名思义的一些方法,用不用皆可,它们的作用是可以帮助系统优化资源的使用
*
* DataPackageView - DataPackage 对象的只读版本,从剪切板获取数据或者分享目标接收数据均通过此对象来获取 DataPackage 对象的数据
* AvailableFormats - DataPackage 中数据所包含的所有格式
* Contains() - 是否包含指定格式的数据
* Properties - 返回 DataPackagePropertySetView 对象,就是由分享源所设置的一些信息
* Properties.Title - 分享数据的标题
* Properties.Description - 分享数据的描述
* Properties.Thumbnail - 分享数据的缩略图
* ApplicationName - 分享源的 app 的名称
* PackageFamilyName - 分享源的 app 的包名
* ApplicationListingUri - 分享源的 app 在商店中的地址
* ContentSourceApplicationLink - 内容源的 ApplicationLink
* ContentSourceWebLink - 内容源的 WebLink
* Square30x30Logo - 分享源的 app 的 30 x 30 的 logo
* LogoBackgroundColor - 分享源的 app 的 logo 的背景色
* FileTypes - 获取分享数据中包含的文件类型
* GetTextAsync(), GetWebLinkAsync(), GetApplicationLinkAsync(), GetHtmlFormatAsync(), GetBitmapAsync(), GetStorageItemsAsync(), GetDataAsync(), GetResourceMapAsync() - 获取分享过来的各种格式的数据,详细用法见下面的相关 demo(注:一个 DataPackage 可以有多种不同格式的数据)
*/ using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.DataTransfer;
using Windows.ApplicationModel.DataTransfer.ShareTarget;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Core;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation; namespace MyShareTarget
{
public sealed partial class MainPage : Page
{
private ShareOperation _shareOperation; public MainPage()
{
this.InitializeComponent();
} protected async override void OnNavigatedTo(NavigationEventArgs e)
{
// 获取分享激活 app 时的事件参数 ShareTargetActivatedEventArgs,可在 App.xaml.cs 的 override void OnShareTargetActivated(ShareTargetActivatedEventArgs args) 中拿到
ShareTargetActivatedEventArgs shareTargetActivated = e.Parameter as ShareTargetActivatedEventArgs;
if (shareTargetActivated == null)
{
lblMsg.Text = "为了演示分享目标,请从分享源激活本程序";
return;
}
// 获取分享的 ShareOperation 对象
_shareOperation = shareTargetActivated.ShareOperation; // 异步获取分享数据
await Task.Factory.StartNew(async () =>
{
// 显示分享数据的相关信息
OutputMessage("Title: " + _shareOperation.Data.Properties.Title);
OutputMessage("Description: " + _shareOperation.Data.Properties.Description);
OutputMessage("ApplicationName: " + _shareOperation.Data.Properties.ApplicationName);
OutputMessage("PackageFamilyName: " + _shareOperation.Data.Properties.PackageFamilyName);
OutputMessage("ApplicationListingUri: " + _shareOperation.Data.Properties.ApplicationListingUri);
OutputMessage("ContentSourceApplicationLink: " + _shareOperation.Data.Properties.ContentSourceApplicationLink);
OutputMessage("ContentSourceWebLink: " + _shareOperation.Data.Properties.ContentSourceWebLink);
OutputMessage("LogoBackgroundColor: " + _shareOperation.Data.Properties.LogoBackgroundColor);
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
IRandomAccessStreamWithContentType logoStream = await _shareOperation.Data.Properties.Square30x30Logo.OpenReadAsync();
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(logoStream);
imgLogo.Source = bitmapImage;
});
OutputMessage(""); // 如果分享数据中包含 Text 格式数据,则显示之
if (_shareOperation.Data.Contains(StandardDataFormats.Text))
{
try
{
var text = await _shareOperation.Data.GetTextAsync();
OutputMessage("Text: " + text);
}
catch (Exception ex)
{
OutputMessage(ex.ToString());
}
} // 如果分享数据中包含 WebLink 格式数据,则显示之
if (_shareOperation.Data.Contains(StandardDataFormats.WebLink))
{
try
{
var uri = await _shareOperation.Data.GetWebLinkAsync();
OutputMessage("WebLink: " + uri.AbsoluteUri);
}
catch (Exception ex)
{
OutputMessage(ex.ToString());
}
} // 如果分享数据中包含 ApplicationLink 格式数据,则显示之
if (_shareOperation.Data.Contains(StandardDataFormats.ApplicationLink))
{
try
{
var uri = await _shareOperation.Data.GetApplicationLinkAsync();
OutputMessage("ApplicationLink: " + uri.AbsoluteUri);
}
catch (Exception ex)
{
OutputMessage(ex.ToString());
}
} // 如果分享数据中包含 Bitmap 格式数据,则显示之
if (_shareOperation.Data.Contains(StandardDataFormats.Bitmap))
{
try
{
IRandomAccessStreamReference stream = await _shareOperation.Data.GetBitmapAsync();
ShowBitmap(stream); ShowThumbnail(_shareOperation.Data.Properties.Thumbnail);
}
catch (Exception ex)
{
OutputMessage(ex.ToString());
}
} // 如果分享数据中包含 Html 格式数据,则显示之
if (_shareOperation.Data.Contains(StandardDataFormats.Html))
{
try
{
// 获取 html 数据
var html = await _shareOperation.Data.GetHtmlFormatAsync();
OutputMessage("Html: " + html); // 获取 html 中包含的本地资源的引用(如引用的本地图片等),其数据在分享源的 DataPackage.ResourceMap 中设置
IReadOnlyDictionary<string, RandomAccessStreamReference> sharedResourceMap = await _shareOperation.Data.GetResourceMapAsync();
}
catch (Exception ex)
{
OutputMessage(ex.ToString());
}
} // 如果分享数据中包含 StorageItems 格式数据,则显示之
if (_shareOperation.Data.Contains(StandardDataFormats.StorageItems))
{
try
{
var storageItems = await _shareOperation.Data.GetStorageItemsAsync();
foreach (var storageItem in storageItems)
{
OutputMessage("storageItem: " + storageItem.Path);
}
}
catch (Exception ex)
{
OutputMessage(ex.ToString());
}
} // 如果分享数据中包含“http://webabcd/sharedemo”格式数据,则显示之
if (_shareOperation.Data.Contains("http://webabcd/sharedemo"))
{
try
{
var customData = await _shareOperation.Data.GetTextAsync("http://webabcd/sharedemo");
OutputMessage("Custom Data: " + customData);
}
catch (Exception ex)
{
OutputMessage(ex.ToString());
}
}
});
} // 在 UI 上输出指定的信息
async private void OutputMessage(string message)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
lblMsg.Text += message;
lblMsg.Text += Environment.NewLine;
});
} // 显示图片
async private void ShowThumbnail(IRandomAccessStreamReference thumbnail)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
if (thumbnail != null)
{
IRandomAccessStreamWithContentType thumbnailStream = await thumbnail.OpenReadAsync();
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(thumbnailStream); imgThumbnail.Source = bitmapImage;
}
});
} // 显示图片
async private void ShowBitmap(IRandomAccessStreamReference bitmap)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
if (bitmap != null)
{
IRandomAccessStreamWithContentType bitmapStream = await bitmap.OpenReadAsync();
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(bitmapStream); imgBitmap.Source = bitmapImage;
}
});
} /*
* 关于 QuickLink 的相关说明如下
* 注:经测试,在我的 windows 10 环境中,此部分内容无效(应该是 windows 10 已经不再支持 QuickLink 了)
*
* ShareOperation - 分享操作的相关信息
* QuickLinkId - 如果是 QuickLink 激活的,此属性可获取此 QuickLink 的 Id
* RemoveThisQuickLink() - 如果是 QuickLink 激活的,此方法可删除此 QuickLink
* ReportCompleted(QuickLink quicklink) - 指定一个需要增加的 QuickLink 对象,然后通知系统分享操作已经完成(会自动关闭分享面板)
*
* QuickLink - 预定义了一些数据,指向相应分享目标的一个快捷方式,其会出现在分享面板的上部
* Id - 预定义数据
* Title - QuickLink 出现在分享面板上的时候所显示的名称
* Thumbnail - QuickLink 出现在分享面板上的时候所显示的图标
* SupportedDataFormats - 分享数据中所包含的数据格式与此定义相匹配(有一个匹配即可),QuickLink 才会出现在分享面板上
* SupportedFileTypes - 分享数据中所包含的文件的扩展名与此定义的相匹配(有一个匹配即可),QuickLink 才会出现在分享面板上
*
* QuickLink 的适用场景举例
* 1、当用户总是分享信息到某一个邮件地址时,便可以在分享目标中以此邮件地址为 QuickLinkId 生成一个 QuickLink
* 2、下回用户再分享时,此 QuickLink 就会出现在分享面板上,用户在分享面板上可以点击此 QuickLink 激活分享目标
* 3、分享目标被 QuickLink 激活后便可以通过 ShareOperation 对象获取到 QuickLink,然后就可以拿到用户需要分享到的邮件地址(就是 QuickLink 的 Id),从而避免用户输入此邮件地址,从而方便用户的分享操作
*/ // 演示如何增加一个 QuickLink
private async void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
OutputMessage("QuickLinkId: " + _shareOperation.QuickLinkId); QuickLink quickLink = new QuickLink
{
Id = "wanglei@email.com",
Title = "分享到邮件: wanglei@email.com", // QuickLink 在分享面板上显示的内容 SupportedFileTypes = { "*" },
SupportedDataFormats =
{
StandardDataFormats.Text,
StandardDataFormats.WebLink,
StandardDataFormats.ApplicationLink,
StandardDataFormats.Bitmap,
StandardDataFormats.StorageItems,
StandardDataFormats.Html,
"http://webabcd/sharedemo"
}
}; try
{
// 设置 QuickLink 在分享面板上显示的图标
StorageFile iconFile = await Package.Current.InstalledLocation.CreateFileAsync("Assets\\StoreLogo.png", CreationCollisionOption.OpenIfExists);
quickLink.Thumbnail = RandomAccessStreamReference.CreateFromFile(iconFile); // 完成分享操作,同时在分享面板上增加一个指定的 QuickLink
_shareOperation.ReportCompleted(quickLink);
}
catch (Exception ex)
{
OutputMessage(ex.ToString());
}
}
}
}

OK
[源码下载]