Windows 8实用窍门系列:14.windows 8中粘贴板(剪切板)的使用
在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+"\r"; } } }
第五:我们贴出前台代码如下
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <!--复制文本--> <TextBox x:Name="SourceText" HorizontalAlignment="Left" Margin="96,50,0,0" TextWrapping="Wrap" Text="复制源" VerticalAlignment="Top" Width="178"/> <Button x:Name="CopyText" Content="复制文本" HorizontalAlignment="Left" Margin="367,50,0,0" VerticalAlignment="Top" Click="CopyText_Click"/> <Button x:Name="PasteText" Content="粘贴文本" HorizontalAlignment="Left" Margin="545,50,0,0" VerticalAlignment="Top" Click="PasteText_Click"/> <TextBlock x:Name="TargetText" HorizontalAlignment="Left" Margin="690,50,0,0" TextWrapping="Wrap" Text="显示文本" VerticalAlignment="Top" Height="32" Width="170"/> <!--复制图片--> <Image x:Name="SourceImage" Source="Assets/iphone0426_006.jpg" Margin="96,150,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="178" Height="178"/> <Button x:Name="CopyImage" Content="复制图片" HorizontalAlignment="Left" Margin="367,150,0,0" VerticalAlignment="Top" Click="CopyImage_Click"/> <Button x:Name="PasteImage" Content="粘贴图片" HorizontalAlignment="Left" Margin="545,150,0,0" VerticalAlignment="Top" Click="PasteImage_Click"/> <Image x:Name="TargetImage" HorizontalAlignment="Left" Margin="690,150,0,0" VerticalAlignment="Top" Width="170" Height="170"/> <!--复制HTML--> <WebView x:Name="SourceHtml" HorizontalAlignment="Left" Source="http://wap.easou.com/" Margin="96,350,0,0" VerticalAlignment="Top" Width="266" Height="278"/> <Button x:Name="CopyHtml" Content="复制HTML" HorizontalAlignment="Left" Margin="367,350,0,0" VerticalAlignment="Top" Click="CopyHtml_Click"/> <Button x:Name="PasteHtml" Content="粘贴HTML" HorizontalAlignment="Left" Margin="545,350,0,0" VerticalAlignment="Top" Click="PasteHtml_Click"/> <WebView x:Name="TargetHtml" HorizontalAlignment="Left" Margin="690,350,0,0" VerticalAlignment="Top" Width="270" Height="278"/> <!--复制文件--> <Button x:Name="CopyFile" Content="复制文件" HorizontalAlignment="Left" Margin="367,650,0,0" VerticalAlignment="Top" Click="CopyFile_Click"/> <Button x:Name="PasteFile" Content="粘贴文件" HorizontalAlignment="Left" Margin="545,650,0,0" VerticalAlignment="Top" Click="PasteFile_Click"/> <TextBlock x:Name="TargetFile" HorizontalAlignment="Left" Margin="690,656,0,0" TextWrapping="Wrap" Text="显示文件拷贝信息" VerticalAlignment="Top" Height="55" Width="450"/> </Grid>
第六:因为要复制文件到“库/文档"文件夹下面,所以需要设置如下两处,具体参考http://www.cnblogs.com/chengxingliang/archive/2012/12/17/2819568.html
设置文件后缀为jpg的可访问。
最后如需源码请点击 Win8ClipBoard2.rar ,效果图如下: