Getting Started with Windows 8 Development (14) use of the Pasteboard (clipboard) in Windows 8

Source: Internet
Author: User

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"; }
            }
        }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.