Clipboard Clipboard Clipboard Clipboard Class

Gets and sets information from the clipboard object.

Syntax

Declaration

public static class Clipboardpublic static class ClipboardPublic Static Class Clipboardpublic static class Clipboard

Remarks

Because the Clipboard class is a static class, you don't need to create an instance of it to access its methods.

Note

: This class is not agile, which means that you need to consider its threading model and marshaling behavior. For more info, see Threading and Marshaling (C++/CX) and Using Windows Runtime objects in a multithreaded environment (.NET).

You can only access the clipboard when the calling application is in focus on the UI thread (e.g., when it's CoreWindow is active). Use the Activated event to track the state of the calling app.

Examples

The following sample shows how to track changes to the Clipboard. If the Clipboard has text, the code adds the text to an HTML page.

Windows.ApplicationModel.DataTransfer.Clipboard.addEventListener("contentchanged", function (event) {
    var dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.getContent();
    if (dataPackageView.contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.text)) {
        dataPackageView.getTextAsync().then(function (text) {
            // To output the text from this example, you need an HTML element
            // with an id of "output".
            document.getElementById("output").innerText = "Clipboard now contains: " + text;
        });
    }
});

Methods summary

Removes all data from the Clipboard.

Adds the content to the Clipboard and releases the DataPackage object from the source app. This method allows the content to remain available after the application shuts down.

Gets the current content that is stored in the clipboard object.

Sets the current content that is stored in the clipboard object.

Events summary

Occurs when the data stored in the Clipboard changes.

Methods

  • Clear()
    Clear()
    Clear()
    Clear()

    Removes all data from the Clipboard.

    public static void Clear()public static void Clear()Public Static Function Clear() As voidpublic static void Clear()

    Remarks

    Use the Clear() method when you want to cancel an action that put data on the Clipboard.

  • Flush()
    Flush()
    Flush()
    Flush()

    Adds the content to the Clipboard and releases the DataPackage object from the source app. This method allows the content to remain available after the application shuts down.

    public static void Flush()public static void Flush()Public Static Function Flush() As voidpublic static void Flush()

    Remarks

    When you call the Flush() method, the content of the DataPackage is added to the Clipboard and then released from the source app. This action makes the content available on the Clipboard even after the source app is no longer available.

  • GetContent()
    GetContent()
    GetContent()
    GetContent()

    Gets the current content that is stored in the clipboard object.

    public static DataPackageView GetContent()public static DataPackageView GetContent()Public Static Function GetContent() As DataPackageViewpublic static DataPackageView GetContent()

    Returns

    Remarks

    If the GetContent call is successful, you can use the DataPackageView it returns to get information such as the file types it supports and any properties that the source app assigned through the DataPackagePropertySet class.

    Examples

    var dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.getContent();
    if (dataPackageView.contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.text)) {
        dataPackageView.getTextAsync().then(function (text) {
            // To output the text from this example, you need an HTML element
            // with an id of "output".
            document.getElementById("output").innerText = "Clipboard now contains: " + text;
        });
    }
    
  • SetContent(DataPackage)
    SetContent(DataPackage)
    SetContent(DataPackage)
    SetContent(DataPackage)

    Sets the current content that is stored in the clipboard object.

    public static void SetContent(DataPackage content)public static void SetContent(DataPackage content)Public Static Function SetContent(content As DataPackage) As voidpublic static void SetContent(DataPackage content)

    Parameters

    Remarks

    Use this method after creating and defining a DataPackage with the data you want to put on the clipboard. Call this method only when the application is in the foreground, or when a debugger is attached.

Events

  • ContentChanged
    ContentChanged
    ContentChanged
    ContentChanged

    Occurs when the data stored in the Clipboard changes.

    public static event EventHandler ContentChangedpublic static event EventHandler ContentChangedPublic Static Event ContentChangedpublic static event EventHandler ContentChanged

    Remarks

    This event is helpful in situations when your app contains logic that varies depending on the contents on the clipboard. For example, your app might include a Paste button, which is disabled unless the Clipboard contains content.

    Examples

    The following example shows how to track changes to the Clipboard. The first code snippet registers a handler for the ContentChanged event. The second code snippet shows the event handler, which enables the app's paste command if the Clipboard contains a bitmap.

       function RegisterEvents() {
    
           // Register a listener for the ContentChanged event. It is called whenever 
           // the clipboard content changes
           Windows.ApplicationModel.DataTransfer.Clipboard.addEventListener(
               "contentchanged", onClipboardContentChanged); 
    
    // TODO: Register other event listeners here.
    
       }
    
    function onClipboardContentChanged(e) {
        var dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.getContent();
        if (dataPackageView.contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.bitmap)) {
            // TODO: Enable the Paste command.
    
        }
    }
    

Device family

Windows 10 (introduced v10.0.10240.0)

API contract

Windows.Foundation.UniversalApiContract (introduced v1)

Attributes

Windows.Foundation.Metadata.ContractVersionAttribute
Windows.Foundation.Metadata.DualApiPartitionAttribute
Windows.Foundation.Metadata.MarshalingBehaviorAttribute
Windows.Foundation.Metadata.StaticAttribute

Details

Assembly

Windows.ApplicationModel.DataTransfer.dll