UWP 共享文件——发送者

时间:2023-03-08 17:57:59

这一节,顾名思义,即使你要共享数据给别人,你是数据的提供者。

分两步即可
1、直接复制代码

protected override void OnNavigatedTo(NavigationEventArgs e)
{
DataTransferManager.GetForCurrentView().DataRequested += Page_DataRequested;
} protected override void OnNavigatedFrom(NavigationEventArgs e)
{
DataTransferManager.GetForCurrentView().DataRequested -= Page_DataRequested;
} void Page_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
var deferral = args.Request.GetDeferral();
//Image
{
args.Request.Data.Properties.Title = "Exif Info Share";
args.Request.Data.Properties.Description = "From Exif Info";// The description is optional.
RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromFile(file);
args.Request.Data.Properties.Thumbnail = imageStreamRef;
args.Request.Data.SetBitmap(imageStreamRef); }
//StorageFile
{
//args.Request.Data.Properties.Title = "Exif Info Share";
//args.Request.Data.Properties.Description = "From Exif Info";// The description is optional.
//List<IStorageItem> files = new List<IStorageItem>();
//files.Add(file);
//args.Request.Data.SetStorageItems(files);
}
deferral.Complete();
}

2、最后

private void Btn_Share_Click(object sender, RoutedEventArgs e)
{
DataTransferManager.ShowShareUI();
}