简体   繁体   中英

UWP share feature not working in Windows 10 Mobile

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 .

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).

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

This is the code in 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();
    }
}

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

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();
}

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);

You have similar issue I had I believe

Have you tried changing

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();    

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

    },  null);
}

I havent checked this code, so it probably wont compile but I have found that I had issue that looks similar to yours, if threads are involved. Take look at my issue here UWP DataTransferManager ShowShareUI() Opens Sharing Dialog with "This app can't share right now" and Closes it Immediately After

I faced the same issue, My share worked good in desktop application but not in mobile. After big struggle I found that the deferral is not working in windows 10 mobile.

So better remove these lines and try. Its working

var deferral = args.Request.GetDeferral();
deferral.Complete();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM