Clipboard API in Windows Phone


Windows Phone by default provides the inbuilt mechanism to support Clipboard Copy, Paste functionalities and shows the proper icon in any TextBox control. But in some case, you may need to handle the copy experience explicitly. - Article authored by Kunal Chowdhury on .

Windows Phone by default provides the inbuilt mechanism to support Clipboard Copy, Paste functionalities and shows the proper icon in any TextBox control. But in some case, you may need to handle the copy experience explicitly.

 

This small post will discuss about the API and the code that you can use in your Windows Phone application. Continue reading.

 

As I said above, the Clipboard functionality is already handled by the operating system in all Windows Phone devices and you don’t have to write a single piece of code normally if it is inside a TextBox or Password box control. But sometime, it is require to provide copy functionality in a List or a View. As you can not paste outside a input box and the OS already handles that, there are no need to handle this operation by code.

 

The Windows Phone SDK provides a class named “Clipboard” present under “System.Windows” namespace and exposes three methods named “ContainsText()”, “GetText()” and “SetText(string text)”.

 

    • ContainsText() - Queries the clipboard for the presence of data in the UnicodeText format and returns true or false.
    • GetText() - If Unicode text data is present on the system clipboard, returns a string that contains the Unicode text data. Otherwise, returns an empty string. You can not directly call this API method.
    • SetText(string text) - Sets Unicode text data to store on the clipboard.

 

An exception of type 'System.Security.SecurityException' occurred in System.Windows.ni.dll but was not handled in user code

 

Please remember that, if you give call to Clipboard.GetText() method, it will throw a SecurityException with the following message: “An exception of type 'System.Security.SecurityException' occurred in System.Windows.ni.dll but was not handled in user code”. Hope, this helps.

 

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.