Configure TextCopy for Blazor WebAssembly
mainThe 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();
}