Win8 共享文本、链接、文件、图片

38 篇文章 1 订阅

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();
            }

        }




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值