Поділитися через


Xamarin.Essentials: Clipboard

The Clipboard class lets you copy and paste text to the system clipboard between applications.

Get started

To start using this API, read the getting started guide for Xamarin.Essentials to ensure the library is properly installed and set up in your projects.

Using Clipboard

Add a reference to Xamarin.Essentials in your class:

using Xamarin.Essentials;

To check if the Clipboard has text currently ready to be pasted:

var hasText = Clipboard.HasText;

To set text to the Clipboard:

await Clipboard.SetTextAsync("Hello World");

To read text from the Clipboard:

var text = await Clipboard.GetTextAsync();

Whenever any of the clipboard's content has changed an event is triggered:

public class ClipboardTest
{
    public ClipboardTest()
    {
        // Register for clipboard changes, be sure to unsubscribe when needed
        Clipboard.ClipboardContentChanged += OnClipboardContentChanged;
    }

    void OnClipboardContentChanged(object sender, EventArgs    e)
    {
        Console.WriteLine($"Last clipboard change at {DateTime.UtcNow:T}";);
    }
}

Tip

Access to the Clipboard must be done on the main user interface thread. See the MainThread API to see how to invoke methods on the main user interface thread.

API

Find more Xamarin videos on Channel 9 and YouTube.