locked
How to use pull operations RRS feed

  • Question

  • Hello all!

    I have some codes, but I don't know if they are correct.

    Please help me to review it, thanks!

            private void ShareExecute(DataRequest request, IList<StorageFile> selectedItems)
            {
                if (selectedItems != null)
                {
                    int selectedCount = selectedItems.Count;
                    if (selectedCount > 0)
                    {

    StorageFile item = selectedItems[0]; if (item != null) { DataPackage requestData = request.Data; //set package id Package package = Package.Current; PackageId packageId = package.Id; if (1 == selectedCount) { requestData.Properties.Title = item.Name; if (null != CommonFunction.ItemImageInfo && null != CommonFunction.ItemImageInfo.ImageInfos) { requestData.Properties.Title = CommonFunction.ItemImageInfo.ImageInfos.ImagePath; } } else { requestData.Properties.Title = string.Format(_title, selectedCount); } requestData.Properties.Description = _description; // The description is optional. request.Data.Properties.FileTypes.Add(StandardDataFormats.StorageItems); request.Data.Properties.FileTypes.Add(StandardDataFormats.Bitmap); request.Data.SetDataProvider(StandardDataFormats.StorageItems, providerRequest => OnDeferredImageRequestedHandler(providerRequest, request, selectedItems)); request.Data.SetDataProvider(CommonDefine.DATA_FORMAT_ID, providerRequest => OnDeferredImageRequestedHandler(providerRequest, request, selectedItems)); request.Data.SetDataProvider(StandardDataFormats.Bitmap, providerRequest => OnDeferredImageRequestedHandler(providerRequest, request, selectedItems)); }

    }

    }

    } private async void OnDeferredImageRequestedHandler(DataProviderRequest providerRequest, DataRequest request, IList<StorageFile> selectedItems, bool bFromClip = false) { DataProviderDeferral deferral = providerRequest.GetDeferral(); List<IStorageItem> imageItems = new List<IStorageItem>(); imageItems.AddRange(selectedItems); List<IStorageItem> _tempFileList = imageItems; StorageFile item = selectedItems[0]; // 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. try { if (imageItems.Count == 1) { if (!bFromClip) { IRandomAccessStream iRandomStream = await item.GetThumbnailAsync( Windows.Storage.FileProperties.ThumbnailMode.SingleItem, 80, Windows.Storage.FileProperties.ThumbnailOptions.UseCurrentScale); RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromStream(iRandomStream); request.Data.Properties.Thumbnail = imageStreamRef; } RandomAccessStreamReference imageStream = RandomAccessStreamReference.CreateFromFile(item); providerRequest.SetData(imageStream); } request.Data.SetStorageItems(imageItems); string dataStr = string.Empty; dataStr = await GetFormatData(_tempFileList);

    request.Data.SetData(CommonDefine.DATA_FORMAT_ID, dataStr); } catch { } finally { deferral.Complete(); } }





    Tuesday, September 17, 2013 12:57 AM

Answers

  • Hi Shirley,

    Yes, you could. The words from MSDN means you must use "SetData" to support delegate function, but not something like when use "SetData" a delegate must be used.

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Shirley-cheng Tuesday, September 17, 2013 1:23 PM
    Tuesday, September 17, 2013 9:18 AM
    Moderator

All replies

  • Hi Shirley,

    I think you could try the code in your computer instead of asking someone here to test for you.

    We could help if you meet some errors while running or you don't understand some part of the code.

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, September 17, 2013 5:52 AM
    Moderator
  • The code is worked, but the sample of msdn is as following:

    private async void OnDeferredImageRequestedHandler(DataProviderRequest request)
    {
        // In this delegate we provide updated Bitmap data using delayed rendering.
    
        if (this.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 this.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();
            }
        }
    }
    

    In the function, it didn't use 'request.Data.SetStorageItems', but 'DataProviderRequest .SetData'.

    I don't know if can I used as this.

    Because there is a word in http://msdn.microsoft.com/en-us/library/windows/apps/hh871375.aspx,

    It is 'Notice that the delegate function uses SetData to add the content, instead of a format-specific function, like SetBitmap or SetStorageItems. Any time you use a delegate function, you must use SetData to supply the content.'

    Tuesday, September 17, 2013 7:02 AM
  • Hi Shirley,

    Yes, you could. The words from MSDN means you must use "SetData" to support delegate function, but not something like when use "SetData" a delegate must be used.

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Shirley-cheng Tuesday, September 17, 2013 1:23 PM
    Tuesday, September 17, 2013 9:18 AM
    Moderator
  • Thank you!

    Tuesday, September 17, 2013 1:23 PM