Windows 8 Store Apps Learning (40) Clipboard operations

Source: Internet
Author: User
Tags flush tostring xmlns

Clipboard: Copy/paste text, HTML, picture, file

Introduced

Re-imagine the Windows 8 Store Apps's clipboard

Clipboard-Shearing Plate

Copy/paste Text

Copy/paste HTML

Copy/Paste Picture

Copy/paste File

Example

1. Demonstration of the basic application of shearing board

Clipboard/demo.xaml

<page x:class= "XamlDemo.Clipboard.Demo" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local= "Using:XamlDemo.Clipboard" xmlns:d= "http://" schemas.microsoft.com/expression/blend/2008 "xmlns:mc=" http://schemas.openxmlformats.org/markup-compatibility/ 2006 "mc:ignorable=" D "> <grid background=" Transparent "> <stackpanel margin=" 0 0 0 "
    
            ; <textblock name= "lblmsg" fontsize= "14.667"/> <button name= "btncopytext" content= "copy a piece of text to the Clipboard" Cl ick= "Btncopytext_click_1" margin= "0 0 0"/> <button name= "Btnpastetext" content= "Paste the text in the Clipboard" Clic k= "Btnpastetext_click_1" margin= "0 0 0"/> <button name= "btnshowavailableformats" content= "Get the Clipboard The format type of the data contained in is "click=" Btnshowavailableformats_click_1 "margin=" 0 0 0 "/> <button name= "Btnclear" ContenT= "Clear all the contents of the Clipboard" click= "Btnclear_click_1" margin= "0 0 0"/> </StackPanel> </Grid> </p Age>

Clipboard/demo.xaml.cs

* * Clipboard-Clipboard * setcontent ()-Save the specified datapackage to the Clipboard * getcontent ()-Get the Datapackage object from the Clipboard * Cl  Ear ()-Clears all data in the Clipboard * Flush ()-Normally, when the app is turned off, the data saved to the Clipboard disappears; When this method is invoked, the data in the Clipboard will not disappear even if the app is turned off * contentchanged -event triggered when data changes in the Clipboard * * datapackage-used to encapsulate Clipboard or sharecontract data (see Sharecontract Demo for details) * Sette XT (), Seturi (), Sethtmlformat (), Setrtf (), SetBitmap (), Setstorageitems (), SetData (), Setdataprovider ()-Set various formats to copy to the Clipboard Data (note: A datapackage can have data in many different formats) * Requestedoperation-action type (datapackageoperation enum: None, Copy, move, Link), not found This property has any effect * * Datapackageview-datapackage the read-only version of the object, fetching data from the Clipboard or sharing the target receive data to obtain the data of the Datapackage object through this object (detailed description see Sharecontrac
T's Demo) * * * using System;
Using Windows.ApplicationModel.DataTransfer;
Using Windows.UI.Xaml;
Using Windows.UI.Xaml.Controls;
    
Using Windows.UI.Xaml.Navigation; Namespace Xamldemo.clipboard {public sealed partial class Demo:page {public DEmo () {this.
        InitializeComponent (); } protected override void Onnavigatedto (NavigationEventArgs e) {Windows.applicationmodel .
        DataTransfer.Clipboard.ContentChanged + = clipboard_contentchanged; } protected override void Onnavigatedfrom (NavigationEventArgs e) {Windows.applicationmod El.
        DataTransfer.Clipboard.ContentChanged-= clipboard_contentchanged; } void Clipboard_contentchanged (object sender, Object e) {Lblmsg.text + = Environment.new
            Line;
        Lblmsg.text + = "The contents of the shearing plate have changed";
            //Copy a piece of text to the clipboard private void Btncopytext_click_1 (object sender, RoutedEventArgs e) {
            Constructs a Datapackage object saved to the Clipboard datapackage datapackage = new Datapackage ();
    
            Datapackage.settext ("I am WEBABCD:" + DateTime.Now.ToString ()); try {windows.applicationmOdel. DataTransfer.Clipboard.SetContent (Datapackage); Save the Datapackage object to the Clipboard Windows.ApplicationModel.DataTransfer.Clipboard.Flush ();
            When this app is closed, the data in the Clipboard remains lblmsg.text = "Copy content to clipboard"; The catch (Exception ex) {lblmsg.text = ex.
            ToString ();
        }///display text data in the Clipboard private async void Btnpastetext_click_1 (object sender, RoutedEventArgs e) {//Get the data in the Clipboard datapackageview Datapackageview = Windows.ApplicationModel.DataTransfer.Cli Pboard.
    
            GetContent ();
                If there is text data in the Clipboard, get and display the text if (Datapackageview.contains (Standarddataformats.text)) {
                    try {string text = await datapackageview.gettextasync ();
                Lblmsg.text = Text; The catch (Exception ex) {lblmsg.text = ex.
ToString ();                } else {lblmsg.text = "no text content in the Clipboard";
        }//Displays the format type of the data contained in the Clipboard, may have a format for standarddataformats enumerations, or it may have custom formatting (see for custom formats: Sharecontract Demo) private void Btnshowavailableformats_click_1 (object sender, RoutedEventArgs e) {Datapackagevi
            EW Datapackageview = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent (); if (Datapackageview!= null && dataPackageView.AvailableFormats.Count > 0) {var av
                Ailableformats = DataPackageView.AvailableFormats.GetEnumerator ();
                    while (Availableformats.movenext ()) {lblmsg.text + = Environment.NewLine;
                Lblmsg.text + = availableformats.current;
            } else {lblmsg.text = "no content in the Clipboard"; }//Clear all data p in the Clipboardrivate void Btnclear_click_1 (object sender, RoutedEventArgs e) {Windows.ApplicationModel.DataTransfer .
        Clipboard.clear (); }
    }
}

2. Demonstrates how to copy HTML data to the Clipboard and how to get HTML data from the Clipboard

Clipboard/copyhtml.xaml

<page
    x:class= "XamlDemo.Clipboard.CopyHtml"
    xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/ Presentation "
    xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "
    xmlns:local=" using: Xamldemo.clipboard "
    xmlns:d=" http://schemas.microsoft.com/expression/blend/2008 "
    xmlns:mc=" http:// schemas.openxmlformats.org/markup-compatibility/2006 "
    mc:ignorable=" D ">
    
    <grid background=" Transparent ">
        <stackpanel margin=" 0 0 0 ">
    
            <textblock name=" lblmsg "fontsize=" 14.667 "/>
    
            <button name= "btncopyhtml" content= "copy a section of HTML to the Clipboard" click= "Btncopyhtml_click_1" margin= "0 0 0"/>
    
            & Lt Button Name= "btnpastehtml" content= "paste the HTML in the Clipboard" click= "Btnpastehtml_click_1" margin= "0 0 0"/> </
    
        stackpanel>
    </Grid>
</Page>

Clipboard/copyhtml.xaml.cs

* * Demonstrates how to copy HTML data to the Clipboard and how to get HTML data from the Clipboard * * Htmlformathelper-pass HTML data in Clipboard or pass HTML in sharecontract Help class * Createhtmlformat ()-encapsulates the HTML string that needs to be passed in order to pass the data in HTML ()-getstaticfragment ()-Unpack the encapsulated HTML data that is passed over and
Gets the initial HTML string that needs to be passed/* using System;
Using Windows.ApplicationModel.DataTransfer;
Using Windows.UI.Xaml;
    
Using Windows.UI.Xaml.Controls;
            Namespace Xamldemo.clipboard {public sealed partial class Copyhtml:page {public copyhtml () { This.
        InitializeComponent ();
            ///Copy HTML string to clipboard private void Btncopyhtml_click_1 (object sender, RoutedEventArgs e) {
            Datapackage datapackage = new Datapackage (); Encapsulates the HTML data that needs to be replicated to copy data to the Clipboard in HTML format Htmlformat = Htmlformathelper.createhtmlformat ("<body>
            I am Webabcd</body> ");
    
            Datapackage.sethtmlformat (Htmlformat);
               try { Windows.ApplicationModel.DataTransfer.Clipboard.SetContent (Datapackage);
            Lblmsg.text = "Content copied to clipboard"; The catch (Exception ex) {lblmsg.text = ex.
            ToString ();  }//Show HTML data in the Clipboard private async void Btnpastehtml_click_1 (object sender, RoutedEventArgs
    
            e) {Datapackageview Datapackageview = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent ();
                if (Datapackageview.contains (standarddataformats.html)) {try
                    {//encapsulated data string Htmlformat = await datapackageview.gethtmlformatasync ();
    
                    The data string htmlfragment = Htmlformathelper.getstaticfragment (Htmlformat) before encapsulation;
                    Lblmsg.text = "Htmlformat (encapsulated data):";
                    Lblmsg.text + = Environment.NewLine; Lblmsg.text + + htmlforMat
                    Lblmsg.text + = Environment.NewLine;
                    Lblmsg.text + = Environment.NewLine;
                    Lblmsg.text + = "htmlfragment (data before encapsulation):";
                    Lblmsg.text + = Environment.NewLine;
                Lblmsg.text + = htmlfragment; The catch (Exception ex) {lblmsg.text = ex.
                ToString ();
            } else {lblmsg.text = "No HTML content in the Clipboard"; }
        }
    }
}

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.