async void OnDeferredImageRequestedHandler(DataProviderRequest request) { // 非同期処理の開始 var deferral = request.GetDeferral(); try { // ファイルの非同期操作 #if WINDOWS_APP // 適当なファイルを用意 var files = (await KnownFolders.PicturesLibrary.GetFilesAsync()) .Where(x => Path.GetExtension(x.Name) == ".jpg") .Take(1); #else // 適当なファイルを用意 var files = (await KnownFolders.CameraRoll.GetFilesAsync()) .Where(x => Path.GetExtension(x.Name) == ".jpg") .Take(1); #endif // 共有するファイル request.SetData(files); } finally { // 非同期処理の終了 deferral.Complete(); } }
private async void OnDeferredImageRequestedHandler(DataProviderRequest request) { // In this delegate we provide updated Bitmap data using delayed rendering. if (_imageFile != null) { // If the delegate is calling any asynchronous operations it needs to acquire // the deferral first. This lets the system know that you are performing some // operations that might take a little longer and that the call to SetData // could happen after the delegate returns. Once you acquired the deferral object // you must call Complete on it after your final call to SetData. DataProviderDeferral deferral = request.GetDeferral(); InMemoryRandomAccessStream inMemoryStream = new InMemoryRandomAccessStream(); // Make sure to always call Complete when finished with the deferral. try { // Decode the image and re-encode it at 50% width and height. IRandomAccessStream imageStream = await _imageFile.OpenAsync(FileAccessMode.Read); BitmapDecoder imageDecoder = await BitmapDecoder.CreateAsync(imageStream); BitmapEncoder imageEncoder = await BitmapEncoder.CreateForTranscodingAsync(inMemoryStream, imageDecoder); imageEncoder.BitmapTransform.ScaledWidth = (uint)(imageDecoder.OrientedPixelWidth * 0.5); imageEncoder.BitmapTransform.ScaledHeight = (uint)(imageDecoder.OrientedPixelHeight * 0.5); await imageEncoder.FlushAsync(); request.SetData(RandomAccessStreamReference.CreateFromStream(inMemoryStream)); } finally { deferral.Complete(); } } }
private void DelayRenderer(DataProviderRequest request) { var deferral = request.GetDeferral(); var tn = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///assets/hololens.png")); request.SetData(tn); deferral.Complete(); }
private void OnDeferredImageRequestedHandler(DataProviderRequest request) { if (_url != null) { // If the delegate is calling any asynchronous operations it needs to acquire // the deferral first. This lets the system know that you are performing some // operations that might take a little longer and that the call to SetData // could happen after the delegate returns. Once you acquired the deferral object // you must call Complete on it after your final call to SetData. DataProviderDeferral deferral = request.GetDeferral(); // Make sure to always call Complete when finished with the deferral. try { request.SetData(_url); } finally { deferral.Complete(); } } }
private void OnShareBitmapData(DataProviderRequest request) { var deferral = request.GetDeferral(); Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => { try { var file = await ApplicationData.Current.TemporaryFolder. CreateFileAsync("TempImage.png", CreationCollisionOption.ReplaceExisting); // Render XAML control to PNG image using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite)) { var renderTargetBitmap = new RenderTargetBitmap(); await renderTargetBitmap.RenderAsync(SharedControl); var pixelBuffer = await renderTargetBitmap.GetPixelsAsync(); var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); encoder.SetPixelData( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)renderTargetBitmap.PixelWidth, (uint)renderTargetBitmap.PixelHeight, 96, 96, pixelBuffer.ToArray()); await encoder.FlushAsync(); } // Share file request.SetData(RandomAccessStreamReference.CreateFromFile(file)); } finally { deferral.Complete(); } }); }
async private void OnDeferredImageRequestedHandler(DataProviderRequest request, StorageFile imageFile) { if (imageFile != null) { // Since this method is using "await" prior to setting the data in DataPackage, // deferral object must be used var deferral = request.GetDeferral(); // Surround try catch to ensure that we always call Complete on defferal. try { using (var imageStream = await imageFile.OpenAsync(FileAccessMode.Read)) { // Decode the image var imageDecoder = await BitmapDecoder.CreateAsync(imageStream); // Re-encode the image at 50% width and height var inMemoryStream = new InMemoryRandomAccessStream(); var imageEncoder = await BitmapEncoder.CreateForTranscodingAsync(inMemoryStream, imageDecoder); imageEncoder.BitmapTransform.ScaledWidth = (uint)(imageDecoder.OrientedPixelWidth * 0.5); imageEncoder.BitmapTransform.ScaledHeight = (uint)(imageDecoder.OrientedPixelHeight * 0.5); await imageEncoder.FlushAsync(); request.SetData(RandomAccessStreamReference.CreateFromStream(inMemoryStream)); } } finally { deferral.Complete(); } await log(OutputText, "Image has been set via deferral"); } else { await log(OutputText, "Error: imageFile is null"); } }
private static void onDeferredReqHdlr(DataProviderRequest request) { DataProviderDeferral deferral = request.GetDeferral(); // Make sure to always call Complete when done with the deferral. try { } finally { deferral.Complete(); } }
private async void OnDeferredImageRequestedHandler(DataProviderRequest request) { // Request deferral to wait for async calls DataProviderDeferral deferral = request.GetDeferral(); // XAML objects can only be accessed on the UI thread, and the call may come in on a background thread await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => { try { RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(); InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream(); // Render to an image at the current system scale and retrieve pixel contents await renderTargetBitmap.RenderAsync(collageCanvas); var pixelBuffer = await renderTargetBitmap.GetPixelsAsync(); // Encode image to an in-memory stream var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); encoder.SetPixelData( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)renderTargetBitmap.PixelWidth, (uint)renderTargetBitmap.PixelHeight, DisplayInformation.GetForCurrentView().LogicalDpi, DisplayInformation.GetForCurrentView().LogicalDpi, pixelBuffer.ToArray()); await encoder.FlushAsync(); // Set content of the DataProviderRequest to the encoded image in memory request.SetData(RandomAccessStreamReference.CreateFromStream(stream)); } finally { deferral.Complete(); } }); }
private void OnDeferredTextRequestedHandler(DataProviderRequest providerRequest) { string text = "Hello World delayed"; providerRequest.SetData(text); }