zoukankan      html  css  js  c++  java
  • UWP 共享文件——接收者

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

    很简单(参考Windows on GithubWindows-universal-samplesSamplesShareTarget)

    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!

  • 相关阅读:
    MySQL SQL语句总结
    JavaFx通用查询、跳转创建、修改页面、关闭舞台工具类
    反射工具类
    SpringBoot将数据写入word模板,生成word文档
    vscode设置中文的方法
    Flex 布局
    处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表中有一个错误模块“ManagedPipelineHandler” •使用了托管的处理程序,但是未安装或未完整安装 ASP.NET。
    第一周助教总结
    JS时间转换
    ES 安装常见错误
  • 原文地址:https://www.cnblogs.com/hupo376787/p/7620251.html
Copyright © 2011-2022 走看看