DataPackage.SetDataProvider(String, DataProviderHandler) 方法

定义

设置委托以处理来自目标应用的请求。

public:
 virtual void SetDataProvider(Platform::String ^ formatId, DataProviderHandler ^ delayRenderer) = SetDataProvider;
void SetDataProvider(winrt::hstring const& formatId, DataProviderHandler const& delayRenderer);
public void SetDataProvider(string formatId, DataProviderHandler delayRenderer);
function setDataProvider(formatId, delayRenderer)
Public Sub SetDataProvider (formatId As String, delayRenderer As DataProviderHandler)

参数

formatId
String

Platform::String

winrt::hstring

指定数据的格式。 建议使用 StandardDataFormats 类设置此值。

delayRenderer
DataProviderHandler

负责处理来自目标应用的请求的委托。

示例

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media.Imaging;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media.Imaging;
using Windows.ApplicationModel.DataTransfer;
using Windows.Graphics.Imaging;

namespace ShareMainBetaCS
{
    public sealed partial class ShareFiles : Page
    {
        private StorageFile imageFile = null;
        private RandomAccessStreamReference imageStreamRef = null;
        private RandomAccessStreamReference dataPackageThumbnail = null;
        private IRandomAccessStream imageStream = null;

        public ShareFiles()
        {
            this.InitializeComponent();
            pickImageButton.Click += new RoutedEventHandler(pickImageButton_Click);
            this.ShareSourceLoad();
        }



        async void pickImageButton_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker imagePicker = new FileOpenPicker
            {
                ViewMode = PickerViewMode.Thumbnail,
                SuggestedStartLocation = PickerLocationId.PicturesLibrary,
                FileTypeFilter = { ".jpg", ".png", ".bmp", ".gif", ".tif" }
            };

            this.imageFile = await imagePicker.PickSingleFileAsync();

            if (this.imageFile != null)
            {
                this.imageStreamRef = RandomAccessStreamReference.CreateFromFile(this.imageFile);
                this.dataPackageThumbnail = this.imageStreamRef;
                this.imageStream = await this.imageFile.OpenAsync(FileAccessMode.Read);
            }

        }

        public void ShareSourceLoad()
        {
            DataTransferManager datatransferManager;
            datatransferManager = DataTransferManager.GetForCurrentView();
            datatransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.DataRequested);
        }

        void DataRequested(DataTransferManager sender, DataRequestedEventArgs e)
        {
            e.Request.Data.Properties.Title = "Hello World!";
            e.Request.Data.Properties.Description = "This example shows how to use delayed sharing.";
            if (this.dataPackageThumbnail != null)
            {
                e.Request.Data.Properties.Thumbnail = this.dataPackageThumbnail;
            }
            e.Request.Data.SetDataProvider(StandardDataFormats.Bitmap, 
                    new DataProviderHandler(this.OnDeferredImageRequestedHandler));

        }
        async void OnDeferredImageRequestedHandler(DataProviderRequest request)
        {
            // Here we provide updated Bitmap data using delayed rendering
            if (this.imageStream != null)
            {
                DataProviderDeferral deferral = request.GetDeferral();

                try
                {
                    InMemoryRandomAccessStream inMemoryStream = new InMemoryRandomAccessStream();

                    // Decode the image
                    BitmapDecoder imageDecoder = await BitmapDecoder.CreateAsync(this.imageStream);

                    // Re-encode the image at 50% width and height
                    BitmapEncoder imageEncoder = await BitmapEncoder.CreateForTranscodingAsync(inMemoryStream, imageDecoder);
                    imageEncoder.BitmapTransform.ScaledWidth = (uint)(imageDecoder.OrientedPixelHeight * 0.5);
                    imageEncoder.BitmapTransform.ScaledHeight = (uint)(imageDecoder.OrientedPixelHeight * 0.5);
                    await imageEncoder.FlushAsync();

                    request.SetData(RandomAccessStreamReference.CreateFromStream(inMemoryStream));
                }

                catch (Exception ex)
                {
                    // Handle the exception
                }

                finally
                {
                    deferral.Complete();
                }
            }
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
    }
}

注解

如果应用支持特定格式,但在目标应用请求数据之前不希望提供数据,请使用 SetDataProvider 方法。 如果你的应用共享可能需要大量处理时间的内容,我们建议你使用此方法;例如,共享所选的照片、视频或文件。

使用此方法时,需要指定格式和函数。 可以使用 StandardDataFormats 类指定格式,也可以将字符串值用于自定义格式。 函数必须使用 SetData 等方法将数据放入 DataPackage 中。

必须指定要传递给共享目标的 StorageItems 的扩展。

SetDataProvider 方法不同于 DataRequest 类的 GetDeferral 方法。 使用 GetDeferral,源应用可以调用立即将数据放入 DataPackage 对象的函数。 SetDataProvider 方法适用于更复杂的共享操作,其中打包要共享的数据更耗时或资源密集型。

适用于