DataPackage.SetDataProvider(String, DataProviderHandler) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Sets a delegate to handle requests from the target app.
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)
Parameters
- formatId
-
String
Platform::String
winrt::hstring
Specifies the format of the data. We recommend that you set this value by using the StandardDataFormats class.
- delayRenderer
- DataProviderHandler
A delegate that is responsible for processing requests from a target app.
Examples
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)
{
}
}
}
Remarks
Use the SetDataProvider method when your app supports a specific format, but does not want to supply the data until the target app requests it. We encourage you to use this method if your app shares content that can require significant processing time; for example, sharing a selection of photos, videos, or files.
When you use this method, you need to specify a format and a function. You can specify the format by using the StandardDataFormats class, or you can use a string value for a custom format. The function must put data in the DataPackage by using a method like SetData.
You must specify the extension of the StorageItems being passed to the Share Target.
The SetDataProvider method is different from the GetDeferral method of the DataRequest class. With GetDeferral, a source app can call a function that immediately puts the data in the DataPackage object. The SetDataProvider method is for more complex share operations where packaging the data to be shared is more time-intensive or resource-intensive.