- VisualStudio2022插件的安装及使用-编程手把手系列文章
- pprof-在现网场景怎么用
- C#实现的下拉多选框,下拉多选树,多级节点
- 【学习笔记】基础数据结构:猫树
Clipboard API 是一种允许网页读取剪贴板数据或向其中写入数据的API,主要有两个方法:
网上相关的文章基本上都是直接使用 Blazor 的JS互操作特性来实现功能 ,本文除了JS互操作还介绍了使用 ClipLazor 库实现功能的方法,使用第三方库代码会更简洁一点.
JS调用 Clipboard API的代码如下,代码可以直接放到 razor 页面中:
<script>
window.clipboardCopy = {
copyText: function (text) {
navigator.clipboard.writeText(text).then(function () {
alert("Copied to clipboard!");
})
.catch(function (error) {
alert(error);
});
}
};
</script>
在 Razor 组件中注入 JSRuntime,并调用该JS
@* 注入 IJSRuntime 实例,用于和 JavaScript 进行交互 *@
@inject IJSRuntime JsRuntime
// 该方法用于将文本异步复制到剪贴板
private async Task CopyTextToClipboard(string txt)
{
await JsRuntime.InvokeVoidAsync("clipboardCopy.copyText", txt);
}
ClipLazor 是一个库,它为 Blazor 应用程序中的 Clipboard API 提供互操作,本质上是对JS互操作进行了封装.
新建一个 Blazor Web App 项目,开发框架选择 .NET8,在 Client 项目中通过 NuGet 添加 ClipLazor 依赖项.
在 Program.cs 文件中,使用 AddClipboard 方法将服务注册到 IoC 容器,服务端和客户端项目都需要添加:
using ClipLazor.Extention;
//...
builder.Services.AddClipboard();
//...
在服务端项目的 App.razor 文件中添加此脚本标记:
<script src="_content/ClipLazor/clipboard.min.js"></script>
Blazor Web App项目模板把解决方案分成了服务端、客户端两个项目,有交互操作的页面都需要放在客户端项目中,将 Clipboard 注入到 razor 文件中:
@using ClipLazor.Components;
@using ClipLazor.Enums;
@inject IClipLazor Clipboard
检查浏览器是否支持 Clipboard API:
bool isSupported = default;
bool isWritePermitted = default;
bool isReadPermitted = default;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
// 静态渲染期间无法进行js互操作,所以必须在这个周期点执行
if (firstRender)
{
isSupported = await Clipboard.IsClipboardSupported();
isWritePermitted = await Clipboard.IsPermitted(PermissionCommand.Write);
isReadPermitted = await Clipboard.IsPermitted(PermissionCommand.Read);
}
}
简单的复制粘贴文本的操作方法:
string msg = string.Empty;
string txt = string.Empty;
string pastedTxt = string.Empty;
async void CopyTxt(string txt)
{
if (txt.Length > 0 && isSupported)
{
if (isWritePermitted)
{
var isCopied = await Clipboard.WriteTextAsync(txt.AsMemory());
if (isCopied)
{
msg = "Text Copied";
}
else
{
msg = "Couldn't copy the text!.";
}
}
StateHasChanged();
}
}
async void PasteTxt()
{
if (isSupported && isWritePermitted)
{
var pastedText = await Clipboard.ReadTextAsync();
if (pastedText is not null)
{
msg = "Text Pasted";
pastedTxt = pastedText;
}
else
{
msg = "Couldn't paste the text!.";
}
}
StateHasChanged();
}
实际使用时也支持复制粘贴图像或二进制文件,具体代码可以参考 ClipLazor 库的完整示例.
以客户端项目的 Counter.razor 为例:
@page "/counter"
@rendermode InteractiveAuto
//代码:将 Clipboard 注入 到 razor 文件中
<PageTitle>Counter</PageTitle>
<h1>Counter</h1>
<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
<p role="status">Txt: @txt</p>
<p role="status">Msg: @msg</p>
<p role="status">PastedTxt: @pastedTxt</p>
@code {
//代码:检查浏览器是否支持 Clipboard API
private int currentCount = 0;
private async void IncrementCount()
{
currentCount++;
txt = currentCount.ToString();
CopyTxt(txt);
PasteTxt();
}
//代码:简单的复制粘贴文本的操作方法
}
测试结果:
最后此篇关于Blazor调用ClipboardAPI读写剪贴板数据的文章就讲到这里了,如果你想了解更多关于Blazor调用ClipboardAPI读写剪贴板数据的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
为了让我的代码几乎完全用 Jquery 编写,我想用 Jquery 重写 AJAX 调用。 这是从网页到 Tomcat servlet 的调用。 我目前情况的类似代码: var http = new
我想使用 JNI 从 Java 调用 C 函数。在 C 函数中,我想创建一个 JVM 并调用一些 Java 对象。当我尝试创建 JVM 时,JNI_CreateJavaVM 返回 -1。 所以,我想知
环顾四周,我发现从 HTML 调用 Javascript 函数的最佳方法是将函数本身放在 HTML 中,而不是外部 Javascript 文件。所以我一直在网上四处寻找,找到了一些简短的教程,我可以根
我有这个组件: import {Component} from 'angular2/core'; import {UserServices} from '../services/UserService
我正在尝试用 C 实现一个简单的 OpenSSL 客户端/服务器模型,并且对 BIO_* 调用的使用感到好奇,与原始 SSL_* 调用相比,它允许一些不错的功能。 我对此比较陌生,所以我可能会完全错误
我正在处理有关异步调用的难题: 一个 JQuery 函数在用户点击时执行,然后调用一个 php 文件来检查用户输入是否与数据库中已有的信息重叠。如果是这样,则应提示用户确认是否要继续或取消,如果他单击
我有以下类(class)。 public Task { public static Task getInstance(String taskName) { return new
嘿,我正在构建一个小游戏,我正在通过制作一个数字 vector 来创建关卡,该数字 vector 通过枚举与 1-4 种颜色相关联。问题是循环(在 Simon::loadChallenge 中)我将颜
我有一个java spring boot api(数据接收器),客户端调用它来保存一些数据。一旦我完成了数据的持久化,我想进行另一个 api 调用(应该处理持久化的数据 - 数据聚合器),它应该自行异
首先,这涉及桌面应用程序而不是 ASP .Net 应用程序。 我已经为我的项目添加了一个 Web 引用,并构建了各种数据对象,例如 PayerInfo、Address 和 CreditCard。但问题
我如何告诉 FAKE 编译 .fs文件使用 fsc ? 解释如何传递参数的奖励积分,如 -a和 -target:dll . 编辑:我应该澄清一下,我正在尝试在没有 MSBuild/xbuild/.sl
我使用下划线模板配置了一个简单的主干模型和 View 。两个单独的 API 使用完全相同的配置。 API 1 按预期工作。 要重现该问题,请注释掉 API 1 的 URL,并取消注释 API 2 的
我不确定什么是更好的做法或更现实的做法。我希望从头开始创建目录系统,但不确定最佳方法是什么。 我想我在需要显示信息时使用对象,例如 info.php?id=100。有这样的代码用于显示 Game.cl
from datetime import timedelta class A: def __abs__(self): return -self class B1(A):
我在操作此生命游戏示例代码中的数组时遇到问题。 情况: “生命游戏”是约翰·康威发明的一种细胞自动化技术。它由一个细胞网格组成,这些细胞可以根据数学规则生存/死亡/繁殖。该网格中的活细胞和死细胞通过
如果我像这样调用 read() 来读取文件: unsigned char buf[512]; memset(buf, 0, sizeof(unsigned char) * 512); int fd;
我用 C 编写了一个简单的服务器,并希望调用它的功能与调用其他 C 守护程序的功能相同(例如使用 ./ftpd start 调用它并使用 ./ftpd stop 关闭该实例)。显然我遇到的问题是我不知
在 dos 中,当我粘贴此命令时它会起作用: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://google.
在 dos 中,当我粘贴此命令时它会起作用: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://google.
我希望能够从 cmd 在我的 Windows 10 计算机上调用 python3。 我已重新安装 Python3.7 以确保选择“添加到路径”选项,但仍无法调用 python3 并使 CMD 启动 P
我是一名优秀的程序员,十分优秀!