在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的可訪問。
最後如需源碼: http://files.cnblogs.com/chengxingliang/Win8ClipBoard2.rar ,效果圖如下: