Quantcast
Channel: User Liyun Zhang - MSFT - Stack Overflow
Viewing all articles
Browse latest Browse all 444

Answer by Liyun Zhang - MSFT for .NET MAUI Hybrid Blazor Copy to Clipboard

$
0
0

You can jsut use the Maui ClipBoard api to do that:

<button class="btn btn-primary" @onclick="Copy">Copy</button><button class="btn btn-primary" @onclick="Paste">Paste</button><InputText @bind-Value="@value"/>@code{    public string value;    public async void Copy()    {        await Clipboard.Default.SetTextAsync("Copy to Click");    }    public async void Paste()    {        value = await Clipboard.Default.GetTextAsync();    }}

Or you can call the javascript to do that:

@inject IJSRuntime js;<button class="btn btn-primary" @onclick="Copy">Copy</button><button class="btn btn-primary" @onclick="Paste">Paste</button><InputText @bind-Value="@value" />@code {    public string value;    public async void Copy()    {        js.InvokeVoidAsync("navigator.clipboard.writeText", "Copy to clipboard");    }    public async void Paste()    {        //value = await js.InvokeAsync<string>("navigator.clipboard.readText");        // this can't work for android because the navigator.clipboard.readText is not compatible with the Android WebView    }}

Note: The javascript paste will crash on the Android, so using the .net maui clipboard api is better.


Viewing all articles
Browse latest Browse all 444

Trending Articles