UWP共享功能在Windows 10移动版中不起作用 [英] UWP share feature not working in Windows 10 Mobile

查看:32
本文介绍了UWP共享功能在Windows 10移动版中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一个按钮创建了一个非常简单的UWP应用程序.单击它应显示内置的共享弹出窗口以共享 PDF文件.

I have created a very simple UWP application with a single button. Clicking it should show the built-in share popup to share a PDF file.

事实是我可以在Windows 10(台式机)上使用它,但不能在移动设备上使用(弹出窗口不会出现在屏幕上).

The fact is that I have it working for Windows 10 (Desktop) but it doesn't work for mobile (the popup doesn't appear on the screen).

PDF文件以字节数组形式出现(因为它将来自远程服务).

The PDF file comes as a byte array (because it will come from a remote service).

这是 MainPage.xaml.cs

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        DataTransferManager.GetForCurrentView().DataRequested += OnDataRequested;
    }

    protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
        // This should come from a service
        PdfBytes = await Microsoft.Toolkit.Uwp.StorageFileHelper.ReadBytesFromPackagedFileAsync("Document.pdf");
    }

    public byte[] PdfBytes { get; set; }

    private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
    {
        var deferral = args.Request.GetDeferral();

        var si = await StorageFile.CreateStreamedFileAsync("Document.pdf", stream =>
        {
            var writeStream = stream.AsStreamForWrite();
            writeStream.Write(PdfBytes, 0, PdfBytes.Length);
            stream.Dispose();                
        },  null);

        args.Request.Data.Properties.Title = "PDF Document";
        args.Request.Data.Properties.Description = "Some description";
        args.Request.Data.SetStorageItems(new IStorageItem[] { si });
        deferral.Complete();
    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        DataTransferManager.ShowShareUI();
    }
}

对吗?如果不是,我应该如何共享PDF(从字节开始)?

Is it correct? If it's not, how should I share the PDF (from its bytes)?

推荐答案

感谢您的反馈.似乎 CreateStreamedFileA不起作用

Thank you for your feedback. It seems that CreateStreamedFileAsync method does not work properly with Share contract in Mobile. We've logged this issue internally and I will update here once there is any progress.

目前,作为一种解决方法,您可以将文件存储在

For now, as a workaround, you can store the file in TemporaryFolder first and then share it like the following:

private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
    var deferral = args.Request.GetDeferral();

    var tempFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("Document.pdf", CreationCollisionOption.ReplaceExisting);
    await FileIO.WriteBytesAsync(tempFile, PdfBytes);

    args.Request.Data.Properties.Title = "PDF Document";
    args.Request.Data.Properties.Description = "Some description";
    args.Request.Data.SetStorageItems(new IStorageItem[] { tempFile });

    deferral.Complete();
}

临时应用数据存储区是存放您不想在当前应用会话后保留的数据的正确位置.系统可以根据需要删除存储在此位置的数据,以释放空间.您可以将其用于任何中间文件或临时文件.如果您要向Temp中写入大量数据,则最好在初始化应用程序时将其清除,以免系统或用户不得不采取措施释放存储空间.您可以通过以下方式进行此操作:

Temporary app data store is the right place for data that you don’t want persisted after the current app session. The system can delete data stored at this location as needed to free up space. You can use it for any intermediate or temporary files. If you are writing large amounts of data to Temp, it is a good idea to clear it when your app is initialized to avoid the system or the user having to take action to free up storage. And you can do this by calling:

await ApplicationData.ClearAsync(ApplicationDataLocality.Temporary);

这篇关于UWP共享功能在Windows 10移动版中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆