In Windows 8, our Pasteboard holds 4 kinds of information: text, pictures, Web pages, files. In this article we will copy and paste each of these 4 elements, of course, you can also copy the 4 elements externally, and then paste it in the program.
Datapackage: Contains data that the user wants to exchange with another application
Set a relay variable to save the user's value
datapackage dp = new Datapackage ();
First: Let's take a look at the background processing code for copying and pasting text.
<!--copy text-->
private void Copytext_click (object sender, RoutedEventArgs e)
{
DP. SetText (this. Sourcetext.text);
Clipboard.setcontent (DP);
<!--paste text-->
private async void Pastetext_click (object sender, RoutedEventArgs e)
{
var Clipboarddata = Clipboard.getcontent ();
if (Clipboarddata.contains (standarddataformats.text))
{this
. Targettext.text = await clipboarddata.gettextasync ();
}
}
Second: Copy and paste picture background processing code
//<!--Copy picture--> 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);
//<!--paste Picture--> private async void Pasteimage_click (object sender, RoutedEventArgs e) {
var clipboarddata = clipboard.getcontent (); if (Clipboarddata.contains (Standarddataformats.bitmap)) {randomaccessstreamreference img = AW
AIT Clipboarddata.getbitmapasync (); var imgstream = await img.
OpenReadAsync ();
BitmapImage bitmap = new BitmapImage (); Bitmap.
SetSource (Imgstream); This.
Targetimage.source = bitmap; }
}
Third: Copy and paste the HTML background processing code
<!--copy 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);
<!--paste 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);
}
Four: Copy and paste the background code of the file
<!--copy files--> 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);
//<!--paste file--> 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 + file Paste one copy to library \ \ document \ \ + sfile.
Name+ "\ r"; }
}
}