Welcome

首页 / 软件开发 / C# / Windows 8开发入门(十四) windows 8中粘贴板(剪切板)的使用

Windows 8开发入门(十四) windows 8中粘贴板(剪切板)的使用2013-12-02 cnblogs 程兴亮在Windows 8中我们的粘贴板分别保存4种信息:文本、图片、网页、文件。在本文中我们将分别复制和粘 贴这4种元素,当然你也可以在外部复制这4种元素,然后在程序中粘贴出来。

DataPackage:包含用户 希望与另一个应用程序交换的数据

//设置一个中转变量保存用户的值 DataPackage dp = new DataPackage();
第一:我们来看看复制和粘贴文本的后台处理代码 。

//<!--复制文本-->private void CopyText_Click(object sender, RoutedEventArgs e){dp.SetText(this.SourceText.Text);Clipboard.SetContent(dp);}//<!--粘贴文本-->private async void PasteText_Click(object sender, RoutedEventArgs e){var ClipBoardData = Clipboard.GetContent();if (ClipBoardData.Contains(StandardDataFormats.Text)){this.TargetText.Text = await ClipBoardData.GetTextAsync();}}
第二:复制和粘贴图片后台处理代码

//<!--复制图片-->private async void CopyImage_Click(object sender, RoutedEventArgs e){Uri uri = new Uri("ms-appx:///Assets/iphone0426_006.jpg");StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);dp.SetBitmap(RandomAccessStreamReference.CreateFromFile(file));Clipboard.SetContent(dp);}//<!--粘贴图片-->private async void PasteImage_Click(object sender, RoutedEventArgs e){var ClipBoardData = Clipboard.GetContent();if (ClipBoardData.Contains(StandardDataFormats.Bitmap)){RandomAccessStreamReference img = await ClipBoardData.GetBitmapAsync();var imgstream = await img.OpenReadAsync();BitmapImage bitmap = new BitmapImage();bitmap.SetSource(imgstream);this.TargetImage.Source = bitmap;}}
第三:复制和粘贴HTML的后台处理代码

//<!--复制HTML-->private void CopyHtml_Click(object sender, RoutedEventArgs e){string html = HtmlFormatHelper.CreateHtmlFormat(this.SourceHtml.InvokeScript("eval", new string[] { "document.documentElement.outerHTML;" }));dp.SetHtmlFormat(html);Clipboard.SetContent(dp);}//<!--粘贴HTML-->private async void PasteHtml_Click(object sender, RoutedEventArgs e){var ClipBoardData = Clipboard.GetContent();if (ClipBoardData.Contains(StandardDataFormats.Html)){string html = await ClipBoardData.GetHtmlFormatAsync();this.TargetHtml.NavigateToString(html);}}
第四:复制和粘贴文件的后台处理代码

//<!--复制文件-->private async void CopyFile_Click(object sender, RoutedEventArgs e){Uri uri1 = new Uri("ms-appx:///Assets/iphone0426_006.jpg");StorageFile file1 = await StorageFile.GetFileFromApplicationUriAsync(uri1);Uri uri2 = new Uri("ms-appx:///Assets/iphone0426_010.jpg");StorageFile file2 = await StorageFile.GetFileFromApplicationUriAsync(uri2);List<StorageFile> filelist = new List<StorageFile>();filelist.Add(file1);filelist.Add(file2);dp.SetStorageItems(filelist);Clipboard.SetContent(dp);}//<!--粘贴文件-->private async void PasteFile_Click(object sender, RoutedEventArgs e){var ClipBoardData = Clipboard.GetContent();this.TargetFile.Text = "";if (ClipBoardData.Contains(StandardDataFormats.StorageItems)){var filelist = await ClipBoardData.GetStorageItemsAsync();foreach (StorageFile sfile in filelist){StorageFile storageFileCopy = await sfile.CopyAsync(KnownFolders.DocumentsLibrary, sfile.Name,NameCollisionOption.ReplaceExisting);this.TargetFile.Text += sfile.Name + "文件粘贴一份到“库\文档\" + sfile.Name+"
";}}}