1:首先添加下面的引用
// 添加引用
using Windows.ApplicationModel; // 获得本地安装文件时使用 Package
using Windows.ApplicationModel.DataTransfer; // DataTransferManager,通过其他应用程序以编程方式启动内容的交换。
using Windows.Storage; // StorageFile
using Windows.Storage.Streams; // RandomAccessStreamReference, 以输入和输出流的形式这文件提供对数据流的随机访问。
2: 声明
DataTransferManager
对象:
DataTransferManager dtm; // 通过其他应用程序以编程方式启动内容的交换。
在
OnNavigatedTo
中 初始化,并添加响应事件
dtm = DataTransferManager.GetForCurrentView(); // 返回与当前窗口关联的 DataTransferManager 对象。
dtm.DataRequested += dtm_DataRequested; // 在共享操作开始时发生。
在
OnNavigatingFrom
中取消事件绑定
dtm.DataRequested -= dtm_DataRequested; // 在页面跳转走时发生。
3: 实现响应方法
dtm_DataRequested
private async void dtm_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
// 共享文本
//string textSource = "asjdkljskfjlklsdgjskl;djklgj;ljglhjkl;df djfk;jhdkl;gjhd;ljgkkkfjhdkx";
//string textTitle = "my title";
//string textDescription = "this is the discription";
//DataPackage data = args.Request.Data;
初始化
//data.Properties.Title = textTitle;
//data.Properties.Description = textDescription;
//data.SetText(textSource);
// 共享链接
//Uri linkSource = new Uri("http://www.baidu.com");
//string linkTitle = "my title";
//string linkDescription = "this is the discription";
//DataPackage data = args.Request.Data;
//data.Properties.Title = linkTitle;
//data.Properties.Description = linkDescription;
//data.SetUri(linkSource);
// 共享带格式的Content/ HTML
//string HTMLSource = "<strong>this is bold text.</strong> and this is not .<a hert='http://www.baidu.com'>baidu...</a>";
//string HTMLTitle = "my title";
//string HTMLDescription = "this is the discription";
//DataPackage data = args.Request.Data;
//data.Properties.Title = HTMLTitle;
//data.Properties.Description = HTMLDescription;
//data.SetHtmlFormat(HtmlFormatHelper.CreateHtmlFormat(HTMLSource));
// 共享文件
//string FileTitle = "my title";
//string FileDescription = "this is the discription";
//DataPackage data = args.Request.Data;
//data.Properties.Title = FileTitle;
//data.Properties.Description = FileDescription;
//DataRequestDeferral waiter = args.Request.GetDeferral(); // 让您可以与目标应用程序异步交换内容。
//try
//{
// StorageFile textFile = await Package.Current.InstalledLocation.GetFileAsync("a.txt");
// List<IStorageItem> files = new List<IStorageItem>();
// files.Add(textFile);
// data.SetStorageItems(files);
//}
//finally
//{
// waiter.Complete();
//}
// 共享图片方法一: 同上共享文件一样
// 共享图片方法二:
string FileTitle = "my title";
string FileDescription = "this is the discription";
DataPackage data = args.Request.Data;
data.Properties.Title = FileTitle;
data.Properties.Description = FileDescription;
DataRequestDeferral waiter = args.Request.GetDeferral(); // 让您可以与目标应用程序异步交换内容。
try
{
StorageFile image = await Package.Current.InstalledLocation.GetFileAsync("Assets\\un_select.png");
data.Properties.Thumbnail = RandomAccessStreamReference.CreateFromFile(image); // 获取或设置 DataPackage 的缩略图像。
data.SetBitmap(RandomAccessStreamReference.CreateFromFile(image)); // 设置 DataPackage 中包含的位图图像。
List<IStorageFile> files = new List<IStorageFile>();
files.Add(image);
data.SetStorageItems(files);
}
finally
{
waiter.Complete();
}
}