UWP上共享,分为接收者(别人共享数据给你,你接收了,然后在做你的处理)和发送者(你给别人发送数据,就像你的App支持图片共享到微信好友或者朋友圈那样,虽然UWP上的微信并不支持这样子)

很简单(参考Windows on Github\Windows-universal-samples\Samples\ShareTarget)

1、先滚进包清单声明,添加共享目标。再选择你App准备接受的数据格式。
如果你的App只接收图像,就填写一个Bitmap了啦


2、然后滚进App.xaml.cs,重写OnShareTargetActivated
一般建议导航到一个ReceivedSharePage页面来处理,并把 ShareOperation 对象作为附加参数传过去,再在页面上接收这个对象实例。

protected override void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
var rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
Window.Current.Content = rootFrame;
}
rootFrame.Navigate(typeof(ReceivedSharePage), args.ShareOperation);
Window.Current.Activate();
}

3、在ReceivedSharePage的后台,重写 OnNavigatedTo 方法,可以获取导航时传递的参数。
前台放一个Image,显示数据

正如我第一步设置的,我的App可以接受图像Bitmap和文件StorageItems两种,所以在代码里面都要处理一下子的。

private ShareOperation shareOperation;
private IRandomAccessStreamReference sharedBitmapStreamRef;
private IReadOnlyList<IStorageItem> sharedStorageItems; protected override async void OnNavigatedTo(NavigationEventArgs e)
{
// It is recommended to only retrieve the ShareOperation object in the activation handler, return as
// quickly as possible, and retrieve all data from the share target asynchronously. this.shareOperation = (ShareOperation)e.Parameter;
await Task.Factory.StartNew(async () =>
{
// Retrieve the data package content.
// The GetWebLinkAsync(), GetTextAsync(), GetStorageItemsAsync(), etc. APIs will throw if there was an error retrieving the data from the source app.
// In this sample, we just display the error. It is recommended that a share target app handles these in a way appropriate for that particular app.
if (this.shareOperation.Data.Contains(StandardDataFormats.Bitmap))
{
try
{
this.sharedBitmapStreamRef = await this.shareOperation.Data.GetBitmapAsync();
}
catch (Exception ex)
{
textResult.Text = "Failed GetBitmapAsync - " + ex.Message;
}
}
if (this.shareOperation.Data.Contains(StandardDataFormats.StorageItems))
{
try
{
this.sharedStorageItems = await this.shareOperation.Data.GetStorageItemsAsync();
}
catch (Exception ex)
{
textResult.Text = "Failed GetBitmapAsync - " + ex.Message;
}
} // In this sample, we just display the shared data content.
// Get back to the UI thread using the dispatcher.
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
if (this.sharedBitmapStreamRef != null)
{
IRandomAccessStreamWithContentType bitmapStream = await this.sharedBitmapStreamRef.OpenReadAsync();
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(bitmapStream);
imgReceived.Source = bitmapImage;
textResult.Text = "Click OK to continue :)";
//写入记忆文件的位置
(Application.Current as App).localSettings.Values["LastFile"] = this.sharedStorageItems[].Path;
}
else if (this.sharedStorageItems != null)
{
try
{
StorageFile file = await StorageFile.GetFileFromPathAsync(this.sharedStorageItems[].Path);
IRandomAccessStreamWithContentType bitmapStream = await file.OpenReadAsync();
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(bitmapStream);
imgReceived.Source = bitmapImage;
textResult.Text = "Click OK to continue :)";
//写入记忆文件的位置
(Application.Current as App).localSettings.Values["LastFile"] = this.sharedStorageItems[].Path;
}
catch
{
textResult.Text = "Sorry, your shared content is not a standard image file :(";
}
}
});
});
}

4、在处理完成时,你需要调用 ReportCompleted 方法,这个不能少,因为这个方法会告诉系统,你的程序已经处理完共享数据了,这时候共享面板会关闭。
(Exif里面并没有这么做,而是把数据传给了ImageExifPage页面,用来分析图像数据了)

5、测试
找一个App,可以向外共享数据的,比如系统的图片App,或者微软给的sample/ShareSource App
选择图片
【Attention!!!通过图片App发送出来的数据,并不是Bitmap格式,而是作为文件StorageItems格式发送出去了。。。。。。一开始我老是纳闷,用Bitmap咋收不到图片,mmp。。。。。。用ShareSource App可以有很多种选择啦】

OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK!

UWP 共享文件——接收者的更多相关文章

  1. UWP 共享文件——发送者

    这一节,顾名思义,即使你要共享数据给别人,你是数据的提供者.分两步即可1.直接复制代码 protected override void OnNavigatedTo(NavigationEventArg ...

  2. Win10/UWP新特性—SharedStorageAccessManager 共享文件

    首先先给大家推荐一个UWP/Win10开发者群:53078485  里面有很多大婶,还有很多学习资源,欢迎大家来一起讨论Win10开发! 在UWP开发中,微软提供了一个新的特性叫做SharedStor ...

  3. UWP学习记录12-应用到应用的通信

    UWP学习记录12-应用到应用的通信 1.应用间通信 “共享”合约是用户可以在应用之间快速交换数据的一种方式. 例如,用户可能希望使用社交网络应用与其好友共享网页,或者将链接保存在笔记应用中以供日后参 ...

  4. Win10 UWP开发实现Bing翻译

    微软在WP上的发展从原来的Win7到Win8,Win8.1,到现在的Win10 UWP,什么是UWP,UWP即Windows 10 中的Universal Windows Platform简称.即Wi ...

  5. Win10/UWP新特性—Drag&Drop 拖出元素到其他App

    在以前的文章中,写过微软新特性Drag&Drop,当时可能由于处于Win10预览版,使用的VS也是预览版,只实现了从桌面拖拽文件到UWP App中,没能实现从UWP拖拽元素到Desktop A ...

  6. UWP开源项目 LLQNotifier 页面间通信利器(移植EventBus)

    前言 EventBus是一个Android版本的页面间通信库,这个库让页面间的通信变得十分容易且大幅降低了页面之间的耦合.小弟之前玩Android的时候就用得十分顺手,现在玩uwp就觉得应该在这平台也 ...

  7. UWP 拉勾客户端

    前些天, 用 Xamarin.Forms (XF) 将就着写了个拉勾的 UWP 和 Android 的客户端. XF 对 Android  和 IOS 的支持做的很到位, 但是对 UWP 的支持目前仅 ...

  8. Win10/UWP新特性系列—Launcher实现应用间的通信

    UWP中,微软为Windows.System.Launcher启动器新增了很多的功能,以前只能启动App,打开指定扩展名文件,对uri协议的解析,以及当启动的应用没有安装时则会提示前往商店下载等. 如 ...

  9. Win10/UWP新特性系列-GetPublisherCacheFolder

    微软Windows Runtime App拥有很强的安全模型来防止不同App之间的数据获取和共享,也就是我们所说的"沙盒机制",每个App都运行在Windows沙盒中,App之间的 ...

随机推荐

  1. wsimport 命令不是内部命令

    问题: 1. webservice在输入命令的时候wsimport的时候会出现如下错误: wsimport不是内部或者外部命令. 2. javac不是内部或者外部命令 3 java 就可以显示配置成功 ...

  2. iOS 之 UITextField

    UITextField 相关细节处理: 1.  设置leftView , rightView let leftView = UIView() // 设置leftView/rightView之后,勿忘设 ...

  3. 爱上朴实的CSS细节

    英文原文:Learning to Love the Boring Bits of CSS  未来的CSS太让人兴奋了:一方面,是全新的页面布局方式:另一方面,是酷炫的滤镜.颜色等视觉效果.这些CSS, ...

  4. PHP内写css样式

    <1>php的两种输出方式 1,echo: 2,print; 栗子: <?php echo:"你好,我的名字是LHH"; print:"你好,我的名字是 ...

  5. awk巩固练习题

    第1章 awk数组练习题 1.1 文件内容(仅第一行) [root@znix test]# head -1 secure-20161219 access.log ==> secure-20161 ...

  6. Java简单实现UDP和TCP

    TCP实现 TCP协议需要在双方之间建立连接,通过输入输出流来进行数据的交换,建立需要通过三次握手,断开需要四次挥手,保证了数据的完整性,但传输效率也会相应的降低. 简单的TCP实现 //服务端 pu ...

  7. linux-echo

    echo 更新时间: 2017-10-11-11:55:24 echo:打印输出内容 参数选择 -e 激活转义字符 命令:echo 123    ,此命令 就会输出123 命令: echo -e &q ...

  8. ueditor精简插件和减少初次加载文件的方法

    ueditor初次使用的时候加载的文件大小大概有1MB还要多,这个页面的打开速度相对来说是很慢很慢的. 其实通常我们并不需要ueditor的全部功能,通过chromedev工具发现初次加载的时候就调用 ...

  9. spring配置文件一般结构

         xml schema:schema在文档根节点当中通过xmlns对文档当中的命名空间进行申明,第三行代码定义了默认命名空间用于spring bean的定义.xsi命名空间用于为每个文档中指定 ...

  10. 在Java编码中,如何减少bug数量

    众所周知,Java编程语言在IT行业是企业中不可缺少的.不管,从Web应用到Android应用,这款语言已经被广泛用于开发各类应用及代码中的复杂功能.但在编写代码时,bug永远是困扰每一位从业者的头号 ...