Clipboard access was one of the features missed during the transition of Windows Phone 8.x development platform from Silverlight to Windows Runtime. As the result, it was one of the most requested feature on the Windows Platform UserVoice site.

Universal Windows Platform bring this feature back for both desktop and phone apps and it is facilitated by Clipboard class.

Adding information to the clipboard require two steps – creation of DataPackage object and assignment of this object as a clipboard content.

var dataPackage = new DataPackage();
dataPackage.SetText("http://lunarfrog.com");
Clipboard.SetContent(dataPackage);

DataPackage supports various data formats, including plain text, HTML, links and images. The same DataPackage class is used to share data between the Universal apps, and in this scenario, it allows setting a data provider, callback function that will be called when data is requested. In case of the clipboard, data provider function can be setup too, but it will be called as soon as the Clipboard.SetContent() function is invoked.

The same Clipboard class is used to retrieve the clipboard content.

DataPackageView dataPackageView = Clipboard.GetContent();
if (dataPackageView.Contains("Text"))
{
    string text = await dataPackageView.GetTextAsync();
}

In this case GetContentMethod returns an instance of DataPackageView class, a read-only DataPackage view. Available types can be evaluated using Contains function (as in the example) or by iterating AvailableFormats property.

That is it – Clipboard API is simple and straightforward to use; it allow to share information not only between the Universal apps but also between them and the Classic desktop apps.

blog comments powered by Disqus