A cross platform package to copy text to and from the clipboard.
A cross platform package to copy text to and from the clipboard.
Support is available via a Tidelift Subscription.
https://nuget.org/packages/TextCopy/
cs await ClipboardService.SetTextAsync("Text to place in clipboard");snippet source | anchor <!-- endSnippet -->
cs ClipboardService.SetText("Text to place in clipboard");snippet source | anchor <!-- endSnippet -->
cs var text = await ClipboardService.GetTextAsync();snippet source | anchor <!-- endSnippet -->
cs var text = ClipboardService.GetText();snippet source | anchor <!-- endSnippet -->
cs ClipboardService.SetText("");snippet source | anchor <!-- endSnippet -->
cs await ClipboardService.SetTextAsync("");snippet source | anchor <!-- endSnippet -->
In addition to the above static API, there is an instance API exposed:
cs Clipboard clipboard = new(); clipboard.SetText("Text to place in clipboard");snippet source | anchor <!-- endSnippet -->
An instance of
Clipboardcan be injected into
IServiceCollection:
cs serviceCollection.InjectClipboard();snippet source | anchor <!-- endSnippet -->
The instance should be injected by using
IClipboard.
There is also a
InjectMockClipboardthat injects an instance of
MockClipboardwith all methods stubbed out.
Due to the dependency on
JSInteropthe static
ClipboardServiceis not supported on Blazor.
Instead inject an
IClipboard:
cs var builder = WebAssemblyHostBuilder.CreateDefault(); var serviceCollection = builder.Services; serviceCollection.InjectClipboard(); builder.RootComponents.Add("app");snippet source | anchor <!-- endSnippet -->
Then consume it:
```cs public partial class IndexModel : ComponentBase { [Inject] public IClipboard Clipboard { get; set; }
public string Content { get; set; }public Task CopyTextToClipboard() { return Clipboard.SetTextAsync(Content); }
public async Task ReadTextFromClipboard() { Content = await Clipboard.GetTextAsync(); }
} ``` snippet source | anchor <!-- endSnippet -->
Blazor support requires the browser APIs clipboard.readText and clipboard.writeText.
Linux uses xsel to access the clipboard. As such it needs to be installed and callable.
To report a security vulnerability, use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.
Clone designed by Wes Breazell from The Noun Project.