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[0].Path;
}
else if (this.sharedStorageItems != null)
{
try
{
StorageFile file = await StorageFile.GetFileFromPathAsync(this.sharedStorageItems[0].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[0].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!

转载于:https://www.cnblogs.com/hupo376787/p/7620251.html

UWP 共享文件——接收者相关推荐

  1. Go 学习笔记(71)— Go 接口 interface (接口定义、接口实现、接口调用、值接收者、指针接收者)

    1. 接口的定义 接口是和调用方的一种约定,它是一个高度抽象的类型,不用和具体的实现细节绑定在一起.接口要做的是定义好约定,告诉调用方自己可以做什么,但不用知道它的内部实现,这和我们见到的具体的类型如 ...

  2. 群晖共享文件夹局域网通过SMB本地访问

    群晖硬件:DS218+ 系统版本:DSM 7.0-41890 Windows版本:Windows10 操作环境:PC.DS连接同一个路由器 前言:此方法适用于局域网映射,PC和DS由同一个路由器分配I ...

  3. Linux虚拟机和Window本地共享文件夹用于PHP项目调试

    我这里虚拟机Linux版本是centos7 第一步 安装vmware tools 1.VMware Workstation菜单栏中,选择"虚拟机","安装VMware T ...

  4. [UWP]实现一个轻量级的应用内消息通知控件

    [UWP]实现一个轻量级的应用内消息通知控件 原文:[UWP]实现一个轻量级的应用内消息通知控件 在UWP应用开发中,我们常常有向用户发送一些提示性消息的需求.这种时候我们一般会选择MessageDi ...

  5. linux挂在win共享文件

    1.先设置共享文件,再网络中查看,有目录则共享成功 2.yum samba-client (linux系统和win系统共享协议的包) 3.yum install cifs-utils 此外还要安装这个 ...

  6. Linux Mount Windows域用户限制的共享文件夹

    sud现在一直使用linux作为主要的办公os,但是最近公司统一使用windows域服务器了,共享就出现比较打的问题了,原因如下: 1.linux下通常mount windows共享文件夹 Linux ...

  7. linux 内网共享文件夹_在Linux下访问Windows共享文件夹

    说明 以下操作以Ubuntu为例,大家可以参考. 我在Ubuntu 14.04和16.04都试过了. Windows共享文件夹 如果局域网内有一台Windows主机,将指定文件夹设为共享,就可以在局域 ...

  8. CentOS虚拟机和物理机共享文件夹实现

    CentOS虚拟机和物理机共享文件夹实现过程. 一.  安装VMware Tools VMwareTools的安装脚本是要使用到perl的,而CentOS 6自身不带perl,所以需要自己安装.可以自 ...

  9. [UWP小白日记-10]程序启动屏(ios解锁既视感)

    [UWP小白日记-10]程序启动屏(ios解锁既视感) 原文:[UWP小白日记-10]程序启动屏(ios解锁既视感) 讲一下 微软爸爸的开发者大会2016又暴了个表达式动画和Windows.UI.Co ...

最新文章

  1. LTSM 实现多元素时序数据植物健康预测
  2. javascript 控制键盘输入
  3. 7-6 列出连通集 (25 分)(详解)
  4. HYSBZ - 1101——莫比乌斯反演
  5. linux 添加环境变量(php为例)
  6. 多叉树的前序遍历_多叉树的创建和遍历(为Trie树做准备)
  7. 有源rc电压放大器实验报告_为什么将放大器装在示波器探头前端?
  8. string类常用单字符处理函数
  9. 开会不要带没用的记事本、笔
  10. [数据结构]树状数组详解
  11. 经纬度坐标转换为大地2000怎么转换_XOMAP-爬虫[4]-零代码实现将quot;地址quot;转换为“经纬度”坐标及“结构化地址”...
  12. python 根据条件输出_python数据类型、输入输出、运算符、条件判断、循环
  13. sql compare mysql版本_SQL Compare 13免费版
  14. 错误 D8016 “/ZI”和“/GL”命令行选项不兼容
  15. 爬取《战狼2》电影短评论,生成图云
  16. ubuntu18.04 安装java
  17. DLL load failed while importing _cvxcore解决办法
  18. 在国外怎么观看国内网站视频电影电视剧
  19. SAP 科目的 未清项管理的理解
  20. 使用HTML设计网页

热门文章

  1. ReactiveCocoa入门教程--第二部分
  2. Android Content Provider基础
  3. 规划以及安装Exchange2007
  4. JavaEE程序员必读图书大推荐
  5. 新站如何迅速提高流量和被百度收录的技巧绝版
  6. 全手工制作arm-linux交叉编译工具链《一》-转
  7. Java 线程池的复用原理
  8. 【iOS】Image图片属性之Render as Template Image
  9. 蓝桥杯 ALGO-55 算法训练 矩阵加法
  10. PAT 乙级 1014. 福尔摩斯的约会 (20) Java版