Search code examples
clipboardavalonia

How do I copy text to clipboard in Avalonia?


I am trying to introduce a command which would copy the value of a bindable property to the system clipboard.

The only piece of information I could find is a very humble Avalonia clipboard manual suggesting doing it this way:

await Application.Current.Clipboard.SetTextAsync("Hello World!");

But trying to do so causes an error:

Error CS0103 The name 'Application' does not exist in the current context

I have tried to resolving this by importing

using Avalonia;

Which made Application.Current visible yet it doesn't appear to include Clipboard:

Error CS1061 'Application' does not contain a definition for 'Clipboard' and no accessible extension method 'Clipboard' accepting a first argument of type 'Application' could be found (are you missing a using directive or an assembly reference?)

I guess accessing the clipboard might be meant to be done in the view rather than the viewmodel but the above problem appears the same even if I try the same in the view code-behind.


Solution

  • You are looking at the 0.10.x documentation, the current version is Version 11 and it's Clipboard documentation is here:

    https://docs.avaloniaui.net/docs/next/concepts/services/clipboard