共享文件——接收者

Posted 猫叔Vincent的后花园

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了共享文件——接收者相关的知识,希望对你有一定的参考价值。

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!

 

以上是关于共享文件——接收者的主要内容,如果未能解决你的问题,请参考以下文章

SnippetsLab for Mac 1.9 中文共享版 – 强大的代码收藏管理工具

从广播接收器更改片段

无法在片段内查看接收短信

从广播接收器更新片段中的列表视图

共享文件——接收者

两台在不同地点的机器如何连接共享文件最快