Search code examples
windows-8windows-runtimecharms-bar

Trouble Attaching File Programmatically to Email in Windows Metro App C#/XAML using Share Charm


I'm simply trying to attach a file named Document.pdf in the DocumentsLibrary to an email using the Share Charm. My code below works perfectly on the Local Machine:

    private async void OnDataRequestedFiles(DataTransferManager sender, DataRequestedEventArgs e)
    {
        List<IStorageItem> shares = new List<IStorageItem>();
        StorageFile filetoShare = await Windows.Storage.KnownFolders.DocumentsLibrary.GetFileAsync("Document.pdf");

        if (filetoShare != null)
        {
            shares.Add(filetoShare);
            filetoShare = null;
        }

        if (shares != null)
        {
            DataPackage requestData = e.Request.Data;
            requestData.Properties.Title = "Title";
            requestData.Properties.Description = "Description"; // The description is optional.
            requestData.SetStorageItems(shares);
            shares = null;

        }
        else
        {
            e.Request.FailWithDisplayText("File not Found.");
        }
    }

But when I run the exact same code on a Windows Surface Tablet, I get the dreaded "There's nothing to share right now." on the right in the Charms flyout area.

Here's a little more background to help:

  • I'm not looking to use a File Picker...I know the exact file I'm looking for
  • I've enabled the Documents Library Capability in the manifest
  • I've added a File Type Association for pdf in the manifest
  • and yes, the file does exist and is in the Documents Library
  • an email account is properly setup in the Mail App on the surface
  • I can successfully send text emails from the Tablet...just not emails with attachments

Like I said, this works on my Win 8 Development Machine as expected...just not on the Surface. I'm wondering if the Surface has different file or folder permissions?

Thanks for the help...this is driving me CRAZY


Solution

  • I finally figured it out - the problem was that my Event Handler was async (so that I could use await to set the StorageFile variable).

    I solved it by setting the StorageFile variable earlier in my code so that it was already available when the Event Handler was called.

    I still have no idea why it worked on my development machine, but no on the WinRT surface...