private static async Task <DataObject> ToDataObject(DataPackageView src, CancellationToken ct) { var dst = new DataObject(); // WPF has no format for URI therefore text is used for both if (src.Contains(StandardDataFormats.Text)) { dst.SetText(await src.GetTextAsync().AsTask(ct)); } else { var uri = DataPackage.CombineUri( src.Contains(StandardDataFormats.WebLink) ? (await src.GetWebLinkAsync().AsTask(ct)).ToString() : null, src.Contains(StandardDataFormats.ApplicationLink) ? (await src.GetApplicationLinkAsync().AsTask(ct)).ToString() : null, src.Contains(StandardDataFormats.Uri) ? (await src.GetUriAsync().AsTask(ct)).ToString() : null); if (string.IsNullOrEmpty(uri) == false) { dst.SetText(uri); } } if (src.Contains(StandardDataFormats.Html)) { dst.SetData(DataFormats.Html, await src.GetHtmlFormatAsync().AsTask(ct)); } if (src.Contains(StandardDataFormats.Rtf)) { dst.SetData(DataFormats.Rtf, await src.GetRtfAsync().AsTask(ct)); } if (src.Contains(StandardDataFormats.Bitmap)) { var srcStreamRef = await src.GetBitmapAsync().AsTask(ct); var srcStream = await srcStreamRef.OpenReadAsync().AsTask(ct); // We copy the source stream in memory to avoid marshalling issue with async stream // and to make sure to read it async as it built from a RandomAccessStream which might be remote. using var tmp = new MemoryStream(); await srcStream.AsStreamForRead().CopyToAsync(tmp); tmp.Position = 0; var dstBitmap = new BitmapImage(); dstBitmap.BeginInit(); dstBitmap.CreateOptions = BitmapCreateOptions.None; dstBitmap.CacheOption = BitmapCacheOption.OnLoad; // Required for the BitmapImage to internally cache the data (so we can dispose the tmp stream) dstBitmap.StreamSource = tmp; dstBitmap.EndInit(); dst.SetData(DataFormats.Bitmap, dstBitmap, false); } if (src.Contains(StandardDataFormats.StorageItems)) { var files = await src.GetStorageItemsAsync().AsTask(ct); var paths = new StringCollection(); foreach (var item in files) { paths.Add(item.Path); } dst.SetFileDropList(paths); } return(dst); }