Перейти к основному содержимому
Версия: 11.0.0

Clipboard

The Clipboard class enables interacting with the system clipboard, providing functionality for setting and retrieving text, clearing the clipboard, handling data objects and working with different data formats.

к сведению

For a complete, runnable example of these concepts in action, check out the sample application.

The Clipboard can be access through an instance of TopLevel or Window, for more details on accessing TopLevel please visit TopLevel page:

var clipboard = window.Clipboard;

Methods

GetTextAsync()

Retrieves text from the clipboard asynchronously. The resulting value of the task is the text from the clipboard. If the clipboard doesn't contain text or is empty, the method returns null.

Task<string?> GetTextAsync()
примечание

Avalonia clipboard always operates with Unicode text.

SetTextAsync(string? text)

Sets the clipboard text asynchronously and flushes it immediately. This method accepts a string? parameter for a text that needs to be copied. If the provided text is null, the clipboard will be cleared.

Task SetTextAsync(string? text)
примечание

Unlikely different Win32 clipboard APIs, Avalonia clipboard always flushes data and is never delayed.

ClearAsync()

Clears the clipboard asynchronously flushes it immediately.

Task ClearAsync()

SetDataObjectAsync(IDataObject data)

Sets the clipboard content to the specified data object asynchronously. This method accepts an IDataObject parameter. The data object can contain multiple data formats.

Task SetDataObjectAsync(IDataObject data)
примечание

Unlikely different Win32 clipboard APIs, Avalonia clipboard always flushes data and is never delayed.

GetFormatsAsync()

Retrieves the list of formats currently stored in the clipboard asynchronously. The resulting value of the task is an array of string format names.

Task<string[]> GetFormatsAsync()

GetDataAsync(string format)

Retrieves data in the specified format from the clipboard asynchronously. This method returns a Task<object?> that represents the operation. The resulting value of the task is the clipboard data in the specified format. If there's no data in the clipboard in the specified format, the method returns null.

Task<object?> GetDataAsync(string format)

Creating a DataObject to be sent to the clipboard

You can store objects on the clipboard on some platforms with different formats.

C#
private async void CopyButton_OnClick(object? sender, RoutedEventArgs args)
{
var clipboard = TopLevel.GetTopLevel(this)?.Clipboard;
var dataObject = new DataObject();
dataObject.Set(DataFormats.Text, "Hello World");
await clipboard.SetDataObjectAsync(dataObject);
}

Platform compatibility:

FeatureWindowsmacOSLinux x11BrowserAndroidiOS
GetTextAsync✔**
SetTextAsync
ClearAsync
GetFormatsAsync✖*✖*✖*
SetDataObjectAsync✖*✖*✖*
GetDataAsync✖*✖*✖*

* Technically possible, but wasn't implemented yet. Contributions are welcome!

** In the Mozilla browser, the GetTextAsync method only functions after the "Paste" gesture has been triggered, typically by using Ctrl+V.