| 
 | 
	
 
 
  在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的后台处理代码 
 
 
 
        // 
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); 
} 
// 
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 filelist = new List(); 
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"; 
} 
} 
} 
  第五:我们贴出前台代码如下 
 
 
 
     
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  第六:因为要复制文件到“库/文档"文件夹下面,所以需要设置如下两处,具体参考http://www.iyunv.com/chengxingliang/archive/2012/12/17/2819568.html 
  设置文件后缀为jpg的可访问。 
  最后如需源码请点击 Win8ClipBoard2.rar ,效果图如下: 
 
 
   |   
 
 
 
 |