Need help with TextCopy?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

CopyText
236 Stars 20 Forks MIT License 1.0K Commits 4 Opened issues

Description

A cross platform package to copy text to and from the clipboard.

Services available

!
?

Need anything else?

Contributors list

TextCopy

  • AppVeyor: Build status
  • TravisCI: Build status
  • NuGet Status

A cross platform package to copy text to and from the clipboard.

NuGet package

https://nuget.org/packages/TextCopy/

Usage

SetTextAsync

cs
await ClipboardService.SetTextAsync("Text to place in clipboard");
snippet source | anchor <!-- endSnippet -->

SetText

cs
ClipboardService.SetText("Text to place in clipboard");
snippet source | anchor <!-- endSnippet -->

GetTextAsync

cs
var text = await ClipboardService.GetTextAsync();
snippet source | anchor <!-- endSnippet -->

GetText

cs
var text = ClipboardService.GetText();
snippet source | anchor <!-- endSnippet -->

Clearing The Clipboard

cs
ClipboardService.SetText("");
snippet source | anchor <!-- endSnippet -->

cs
await ClipboardService.SetTextAsync("");
snippet source | anchor <!-- endSnippet -->

Instance API

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 -->

Dependency Injection

An instance of

Clipboard
can be injected into
IServiceCollection
:

cs
serviceCollection.InjectClipboard();
snippet source | anchor <!-- endSnippet -->

The instance should be injected by using

IClipboard
.

There is also a

InjectMockClipboard
that injects an instance of
MockClipboard
with all methods stubbed out.

Supported on

  • Windows with .NET Framework 4.6.1 and up
  • Windows with .NET Core 2.0 and up
  • Windows with Mono 5.0 and up
  • OSX with .NET Core 2.0 and up
  • OSX with Mono 5.20.1 and up
  • Linux with .NET Core 2.0 and up
  • Linux with Mono 5.20.1 and up
  • Xamarin.Android 9.0 and up
  • Xamarin.iOS 10.0 and up
  • Universal Windows Platform version 10.0.16299 and up
  • Blazor WebAssembly 5.0 and up

Blazor WebAssembly

Due to the dependency on

JSInterop
the static
ClipboardService
is 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

Linux uses xsel to access the clipboard. As such it needs to be installed and callable.

Icon

Clone designed by Wes Breazell from The Noun Project.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.