我正在编写 Windows 8.1 Universal App 。图像从服务器加载到 GridView 。在单击GridViewItem(图像)时,它应该将该图像共享给WhatsApp等。
单击GridViewItem, ShareSticker 方法执行:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
DataTransferManager.GetForCurrentView().DataRequested += OnShareDataRequested;
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
DataTransferManager.GetForCurrentView().DataRequested -= OnShareDataRequested;
}
List<IStorageItem> storageItemsObject = new List<IStorageItem>();
StorageFile StorageFileObject;
private async void ShareSticker(string productID, string ProductServerURL, string ProductName)
{
try
{
StorageFolder StorageFolderObject = await ApplicationData.Current.LocalFolder.CreateFolderAsync(LocalCache, CreationCollisionOption.OpenIfExists);
if (StorageFolderObject != null)
{
string FileNameSaved = productID;
string extension = Path.GetExtension(ProductServerURL);
FileNameSaved = FileNameSaved + extension;
StorageFileObject = null;
StorageFileObject = await StorageFolderObject.CreateFileAsync(FileNameSaved, CreationCollisionOption.ReplaceExisting);
IRandomAccessStream StorageFileObjectStream = await StorageFileObject.OpenAsync(FileAccessMode.ReadWrite);
HttpClient HttpClientObject = new HttpClient();
HttpClientObject.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
Uri UriObject = new Uri(ProductServerURL);
HttpResponseMessage HttpResponseMessageObject = await HttpClientObject.GetAsync(UriObject);
HttpResponseMessageObject.EnsureSuccessStatusCode();
IBuffer ResponseBuffer = await HttpResponseMessageObject.Content.ReadAsBufferAsync();
DataWriter DataWriterObject;
IOutputStream StorageFileObjectOutputStream;
using (StorageFileObjectOutputStream = StorageFileObjectStream.GetOutputStreamAt(0))
{
DataWriterObject = new DataWriter(StorageFileObjectOutputStream);
DataWriterObject.WriteBuffer(ResponseBuffer);
await DataWriterObject.StoreAsync();
await StorageFileObjectOutputStream.FlushAsync();
}
storageItemsObject.Clear();
storageItemsObject.Add(StorageFileObject);
DataTransferManager.ShowShareUI();
}
}
catch (Exception ex)
{
}
}
private void OnShareDataRequested(DataTransferManager sender, DataRequestedEventArgs _dataRequestedEventArgs)
{
DataRequest request = _dataRequestedEventArgs.Request;
request.Data.Properties.Title = "KeyTreat Sticker";
request.Data.Properties.Description = "KeyTreat Sticker: " + StickerName;
// Because we are making async calls in the DataRequested event handler,
// we need to get the deferral first.
DataRequestDeferral deferral = request.GetDeferral();
// Make sure we always call Complete on the deferral.
try
{
request.Data.SetStorageItems(storageItemsObject);
#if WINDOWS_APP
request.Data.Properties.Thumbnail = RandomAccessStreamReference.CreateFromFile(StorageFileObject);
request.Data.SetBitmap(RandomAccessStreamReference.CreateFromFile(StorageFileObject));
#endif
}
finally
{
deferral.Complete();
}
}
问题: 点击图片后,它会显示“准备分享内容”1秒钟,但没有任何反应。