DataPackage.SetDataProvider(String, DataProviderHandler) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
設定委派以處理來自目標應用程式的要求。
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方法適用于更複雜的共用作業,其中封裝要共用的資料會更耗時或需要大量資源。