locked
Windows 8.1 share doesn't work when Print Screen is added RRS feed

  • Question

  • I'm trying to implement some logical to share the image of my webView and some extras informations. If I do that without capture screen, it works perfectly:

     
          private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e)
            {
                if (await GetShareContent(e.Request))
                {
                    if (String.IsNullOrEmpty(e.Request.Data.Properties.Title))
                    {
                        e.Request.FailWithDisplayText("Nenhum título adicionado");
                    }
                }
            }
        private async Task<bool> GetShareContent(DataRequest request, StorageFile file)
                {
                    bool succeeded = false;
                    string text = "Dados do Arquivo:" + Environment.NewLine + webViewModel.Name;
                    string dataPackageText = text;
        
                    if (!String.IsNullOrEmpty(dataPackageText))
                    {
                        DataPackage requestData = request.Data;
                        requestData.Properties.Title = "Target";
                        requestData.Properties.Description = webViewModel.Name;
                        requestData.SetText(dataPackageText);
                        succeeded = true;
                    }
                    else
                    {
                        request.FailWithDisplayText("Não há nada para compartilhar");
                    }
                    return succeeded;
                }
    But, if I try the same thing justing adding the captured image, it doesn't work, doesn't show any Excepetion, just the message: "Não há nada para compartilhar agora" (There's nothing to share right now)

    I don't know what is going on. The code that doesn't work:

     
      private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e)
        {
            StorageFile file = await captureScreen();
    
            if (await GetShareContent(e.Request, file))
            {
                if (String.IsNullOrEmpty(e.Request.Data.Properties.Title))
                {
                    e.Request.FailWithDisplayText("Nenhum título adicionado");
                }
            }
        }
    
         private async Task<StorageFile> captureScreen()
                {
                    RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
                    await renderTargetBitmap.RenderAsync(webView, (int)webView.Width, (int)webView.Height);
        
                    Image myImage = new Image();
                    myImage.Source = renderTargetBitmap;
        
                    var file = await App.rootDir.CreateFileAsync("screenCapture.jpg", CreationCollisionOption.ReplaceExisting);
                    var pixels = await renderTargetBitmap.GetPixelsAsync();
        
                    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
                        byte[] bytes = pixels.ToArray();
                        encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                             BitmapAlphaMode.Ignore,
                                             (uint)webView.Width, (uint)webView.Height,
                                             96, 96, bytes);
        
                        await encoder.FlushAsync();
                    }
        
                    return file;
                }
    I though it could be happening because the image was not read when the share is called, but I'm using await as it should be. And my jpeg is created perfectly.



    • Edited by Victor Laerte Thursday, October 3, 2013 9:44 PM Formatting
    Thursday, October 3, 2013 9:43 PM

Answers

  • Hi Victor,

    You need to request a deferral in OnDataRequested before you can call await.

    OnDataRequested will return at the await call, ending the sharing request before the data is available. Under the covers, await essentially tells the compiler to split the function into multiple functions. The initial function returns after triggering the awaitable Task, and the part after the await gets called when the Task completes.

    Requesting a deferral will let the share system know that you're not really done with OnDataRequested even though it has returned.

    See How to make asynchronous calls in your DataRequested handler .

    --Rob

    • Marked as answer by Victor Laerte Friday, October 4, 2013 12:53 PM
    Thursday, October 3, 2013 10:00 PM
    Moderator