UWP 共享文件——接收者
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 共享文件——接收者的更多相关文章
- UWP 共享文件——发送者
这一节,顾名思义,即使你要共享数据给别人,你是数据的提供者.分两步即可1.直接复制代码 protected override void OnNavigatedTo(NavigationEventArg ...
- Win10/UWP新特性—SharedStorageAccessManager 共享文件
首先先给大家推荐一个UWP/Win10开发者群:53078485 里面有很多大婶,还有很多学习资源,欢迎大家来一起讨论Win10开发! 在UWP开发中,微软提供了一个新的特性叫做SharedStor ...
- UWP学习记录12-应用到应用的通信
UWP学习记录12-应用到应用的通信 1.应用间通信 “共享”合约是用户可以在应用之间快速交换数据的一种方式. 例如,用户可能希望使用社交网络应用与其好友共享网页,或者将链接保存在笔记应用中以供日后参 ...
- Win10 UWP开发实现Bing翻译
微软在WP上的发展从原来的Win7到Win8,Win8.1,到现在的Win10 UWP,什么是UWP,UWP即Windows 10 中的Universal Windows Platform简称.即Wi ...
- Win10/UWP新特性—Drag&Drop 拖出元素到其他App
在以前的文章中,写过微软新特性Drag&Drop,当时可能由于处于Win10预览版,使用的VS也是预览版,只实现了从桌面拖拽文件到UWP App中,没能实现从UWP拖拽元素到Desktop A ...
- UWP开源项目 LLQNotifier 页面间通信利器(移植EventBus)
前言 EventBus是一个Android版本的页面间通信库,这个库让页面间的通信变得十分容易且大幅降低了页面之间的耦合.小弟之前玩Android的时候就用得十分顺手,现在玩uwp就觉得应该在这平台也 ...
- UWP 拉勾客户端
前些天, 用 Xamarin.Forms (XF) 将就着写了个拉勾的 UWP 和 Android 的客户端. XF 对 Android 和 IOS 的支持做的很到位, 但是对 UWP 的支持目前仅 ...
- Win10/UWP新特性系列—Launcher实现应用间的通信
UWP中,微软为Windows.System.Launcher启动器新增了很多的功能,以前只能启动App,打开指定扩展名文件,对uri协议的解析,以及当启动的应用没有安装时则会提示前往商店下载等. 如 ...
- Win10/UWP新特性系列-GetPublisherCacheFolder
微软Windows Runtime App拥有很强的安全模型来防止不同App之间的数据获取和共享,也就是我们所说的"沙盒机制",每个App都运行在Windows沙盒中,App之间的 ...
随机推荐
- Java:什么是面向对象?
1.首先我们去区分对象的属性和方法. 一).什么是对象? 比如:所有的东西都可以叫做对象.而对象就是提供给我们研究对象,这就是对象. 二).怎么区分"属性"和"方法&qu ...
- C#与SQl数据的对应关系(tinyint、smallint、int、bigint)
SQL C# bigint(sql大小:8byte) ...
- 最长上升子序列(logN算法)
例如:1 7 3 5 9 4 8 一个序列,比如说a[]={1,7,3,5,9,4,8},找出它的最长上升子序列的个数,很明显是4个,可以是{1,3,5,9},{1,3,5,8}或者{1,3,4,8} ...
- 【转载】XSS学习笔记
XSS的分类 非持久型 非持久型XSS也称反射型XSS.具体原理就是当用户提交一段代码的时候,服务端会马上返回页面的执行结果.那么当攻击者让被攻击者提交一个伪装好的带有恶意代码的链接时,服务端也会立刻 ...
- eclipse中删除多余的tomcat server
在eclipse菜单中选择Window--Show View--Server--Servers,打开这个服务窗口,将多余的服务删除即可. 如果每次启动中太卡没反应,那就是服务没选择好,或是端口冲突的原 ...
- Oracle 11g服务器安装详细步骤——图文教程
1.大家可以根据自己的操作系统是多少位(32位或64位)的,到官网下载相应的安装程序,如下图所示. 有一点需要注意,Oracle的安装程序分成2个文件,下载后将2个文件解压到同一目录即可. 2.下载完 ...
- Java微信公众平台开发_07_JSSDK图片上传
一.本节要点 1.获取jsapi_ticket //2.获取getJsapiTicket的接口地址,有效期为7200秒 private static final String GET_JSAPITIC ...
- hadoop启动name失败
namenode失败十分的常见, 1.java.io.EOFException; Host Details : local host is: "hadoop1/192.168.41.134& ...
- Spring ioc与aop的理解
一 spring的特点 1.降低了组件之间的耦合性 ,实现了软件各层之间的解耦 2.可以使用容易提供的众多服务,如事务管理,消息服务等 3.容器提供单例模式支持 4.容器提供了AOP技术,利用它很容易 ...
- SQL命令语句进行大数据查询如何进行优化
SQL 大数据查询如何进行优化? 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索 2.应尽量避免在 where 子句中对字段进行 null 值 ...