.NET MAUI Hybrid Blazor Copy to Clipboard - Stack Overflow

I've found a few resources on how to copy to clipboard within MAUI Native, plus within Blazor Serv

I've found a few resources on how to copy to clipboard within MAUI Native, plus within Blazor Server, but I'm wondering how can you get copy to clipboard working on a Blazor page within a .NET MAUI Hybrid app?

Here's a resource from MS on native: .0

I've found a few resources on how to copy to clipboard within MAUI Native, plus within Blazor Server, but I'm wondering how can you get copy to clipboard working on a Blazor page within a .NET MAUI Hybrid app?

Here's a resource from MS on native: https://learn.microsoft/en-us/dotnet/maui/platform-integration/data/clipboard?view=net-maui-8.0

Share Improve this question asked Nov 19, 2024 at 15:23 Matt S.Matt S. 3232 silver badges13 bronze badges 1
  • Did you try to use the maui clipboard directly? And what problem did you met? – Liyun Zhang - MSFT Commented Nov 20, 2024 at 5:29
Add a comment  | 

1 Answer 1

Reset to default 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 maui clipboard api is better.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742419035a4440310.html

相关推荐

  • .NET MAUI Hybrid Blazor Copy to Clipboard - Stack Overflow

    I've found a few resources on how to copy to clipboard within MAUI Native, plus within Blazor Serv

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信