Clipboard
How to use the Clipboard class of the bit Butil?
Usage
To use the browser Clipboard features you need to inject the Bit.Butil.Clipboard class and use it like this:
@inject Bit.Butil.Clipboard clipboard @code { var text = await clipboard.ReadText(); }
Methods
ReadText:
Requests text from the system clipboard, returning a Promise that is fulfilled with a string containing the clipboard's text once it's available (MDN).
@inject Bit.Butil.Clipboard clipboard <BitButton OnClick="ReadText">ReadText</BitButton> <div>Clipboard text: @clipboardText</div> @code { private string clipboardText = string.Empty; private async Task ReadText() { clipboardText = await clipboard.ReadText(); } }
WriteText:
Requests text from the system clipboard, returning a Promise that is fulfilled with a string containing the clipboard's text once it's available (MDN).
@inject Bit.Butil.Clipboard clipboard <BitTextField @bind-Value="newClipboardText" /> <BitButton OnClick="WriteText">WriteText</BitButton> @code { private string newClipboardText = string.Empty; private async Task WriteText() { await clipboard.WriteText(newClipboardText); } }
Read:
Requests arbitrary data (such as images) from the clipboard, returning a Promise that resolves with an array of ClipboardItem objects containing the clipboard's contents (MDN).
@inject Bit.Butil.Clipboard clipboard <BitButton OnClick="Read">Read</BitButton> @foreach (var item in clipboardItems) { <div>Clipboard MimeType: "@item.MimeType"</div> <div>Clipboard Data: "@System.Text.Encoding.UTF8.GetString(item.Data)"</div> } @code { private ClipboardItem[] clipboardItems = []; private async Task Read() { clipboardItems = await clipboard.Read(); } }
Write:
Writes arbitrary data to the system clipboard, returning a Promise that resolves when the operation completes (MDN).
@inject Bit.Butil.Clipboard clipboard <BitTextField @bind-Value="newText" /> <BitButton OnClick="Write">Write</BitButton> @code { private string newText = string.Empty; private async Task Write() { var data = System.Text.Encoding.UTF8.GetBytes(newText); var item = new ClipboardItem() { MimeType = "text/plain", Data = data }; await clipboard.Write([item]); } }