问题
Requirement: Share a text and image using DataTransferManager
into Facebook in Windows 10.
Problem: Unable to share image.
Below shown is the code I used,
private async void DataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
DataRequestDeferral deferral = args.Request.GetDeferral();
args.Request.Data.Properties.Title = "Sharing sample";
args.Request.Data.SetText("Testing share in universal app");
var imageUri = "http://cdn.vrworld.com/wp-content/uploads/2015/01/microsoft-announces-windows-10_ahab.1920.jpg";
//var storageFile = await StorageFile.CreateStreamedFileFromUriAsync("ShareFile", new Uri(imageUri), null);
//List<IStorageItem> storageItems = new List<IStorageItem>();
//storageItems.Add(storageFile);
//args.Request.Data.SetStorageItems(storageItems);
args.Request.Data.SetBitmap(Windows.Storage.Streams.RandomAccessStreamReference.CreateFromUri(new Uri(imageUri)));
deferral.Complete();
}
When I use SetBitmap
method, only the title and text are being shared. The image is neither displayed in the share panel nor shared to the target app.
When I use SetStorageItems
(see commented code), none of the items are shared. The default "What's on your mind" text appears on the share panel.
Any feedback is appreciated, thank you!
回答1:
Sharing of URI streamed files is unfortunately not supported. Here's how I would go about doing this:
- When the user clicks the share button, start downloading the file
and show some sort of progress if it's a big file. You could also
pre-download the file of course. Set up a
StorageFile
instance containing the file. - Call DataTransferManager.ShowShareUI
- In your
DataRequested
handler, useSetStorageItems
to share theStorageFile
instance.
回答2:
I think you refer to Share Target in UWP You may refer to this URL https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/ShareSource
This sample demonstrates how an app shares content with another app. This sample uses classes from the Windows.ApplicationModel.DataTransfer namespace. Some of the classes you might want to review in more detail are the DataTransferManager class, which you use to initiate a share operation, and the DataPackage class, which you use to package the content. Because each share scenario usually involves two apps—the source app and a target app that receives the content—we recommend you install and deploy the Sharing content target app sample when you install and run this one. This way, you can see how sharing works from end to end.
This sample covers how to share content in a variety of formats, including:
1.Text 2.Web link 3.Application link 4.Images 5.Files 6.Delay-rendered files 7.HTML content 8.Custom data
protected override bool GetShareContent(DataRequest request)
{
bool succeeded = false;
if (this.imageFile != null)
{
DataPackage requestData = request.Data;
requestData.Properties.Title = TitleInputBox.Text;
requestData.Properties.Description = DescriptionInputBox.Text; // The description is optional.
requestData.Properties.ContentSourceApplicationLink = ApplicationLink;
// It's recommended to use both SetBitmap and SetStorageItems for sharing a single image
// since the target app may only support one or the other.
List<IStorageItem> imageItems = new List<IStorageItem>();
imageItems.Add(this.imageFile);
requestData.SetStorageItems(imageItems);
RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromFile(this.imageFile);
requestData.Properties.Thumbnail = imageStreamRef;
requestData.SetBitmap(imageStreamRef);
succeeded = true;
}
else
{
request.FailWithDisplayText("Select an image you would like to share and try again.");
}
return succeeded;
}
来源:https://stackoverflow.com/questions/30051700/unable-to-share-image-using-datatransfermanager-in-windows-10