TextCopy .NET Library

repository·main·Indexed 20 days ago

https://github.com/copytext/textcopy

A cross-platform .NET library for copying and retrieving text from the system clipboard. Supports Windows, OSX, Linux (requires xsel), Mobile (Xamarin.Android/iOS), and Blazor WebAssembly. Provides a static ClipboardService API, an instance-based Clipboard API, and dependency injection integration via IClipboard.

Tokens
936
Snippets
5
Records
6
Agent score
19%

What's inside TextCopy

  1. Configure TextCopy for Blazor WebAssembly

    main

    The static ClipboardService is not supported in Blazor because it requires JSInterop. Instead, you must inject IClipboard into your components.

    Note: Blazor support requires the browser APIs clipboard.readText and clipboard.writeText to be available.

    // In Program.cs
    var builder = WebAssemblyHostBuilder.CreateDefault();
    var serviceCollection = builder.Services;
    serviceCollection.InjectClipboard();
    builder.RootComponents.Add<App>("app");
    
    // In a Blazor Component
    public partial class IndexModel : ComponentBase
    {
        [Inject]
        public IClipboard Clipboard { get; set; }
    
        public string Content { get; set; }
    
        public Task CopyTextToClipboard() =>
            Clipboard.SetTextAsync(Content);
    
        public async Task ReadTextFromClipboard() =>
            Content = await Clipboard.GetTextAsync();
    }
  2. Register Clipboard with Dependency Injection

    main

    You can register the clipboard service in your IServiceCollection to use the instance-based API via dependency injection. Use IClipboard to consume the service. For testing purposes, you can also use InjectMockClipboard to inject a MockClipboard with stubbed methods.

    // Registering the service
    serviceCollection.InjectClipboard();
    
    // For testing (injects MockClipboard)
    serviceCollection.InjectMockClipboard();
  3. Use the static ClipboardService API

    main

    For simple use cases, you can use the static ClipboardService class to interact with the clipboard without managing instances.

    // Copy text to clipboard
    await ClipboardService.SetTextAsync("Text to place in clipboard");
    ClipboardService.SetText("Text to place in clipboard");
    
    // Get text from clipboard
    var text = await ClipboardService.GetTextAsync();
    var text = ClipboardService.GetText();
    
    // Clear the clipboard
    ClipboardService.SetText("");
    await ClipboardService.SetTextAsync("");
  4. Supported Platforms and Linux Requirements

    main

    TextCopy is cross-platform and supports:

    • Windows: .NET Framework 4.6.1+, .NET Core 2.0+, Mono 5.0+
    • OSX: .NET Core 2.0+, Mono 5.20.1+
    • Linux: .NET Core 2.0+, Mono 5.20.1+
    • Mobile: Xamarin.Android 9.0+, Xamarin.iOS 10.0+
    • Web: Blazor WebAssembly 6.0+

    Linux Requirement: Linux users must have xsel installed and available in the system path to access the clipboard.