xamarin.forms - (Android 和 iOS)Xamarin - 如何从 WebView 中获取点击/选定的文本或段落?
问题描述
如何从 WebView 中获取选定的文本范围?是否有诸如 selectedSomething 之类的 WebView 自定义事件?
--
如果没有解决方案;
我可以从 Webview 获取复制的文本/段落吗?复制的是否有事件(可能是自定义)?
--
我很乐意提供任何帮助。
解决方案
在我们选择字符串后,选择会很快消失。该解决方案来自 Xamarin 论坛中的 ColeX。该选择适用于创建自定义渲染器并用 WKWebView 替换默认 webview。
(https://forums.xamarin.com/discussion/comment/396734)
自定义WebView
public class CustomWebView : WebView
{
public static readonly BindableProperty UriProperty = BindableProperty.Create(
propertyName: "Uri",
returnType: typeof(string),
declaringType: typeof(CustomWebView),
defaultValue: default(string));
public string Uri
{
get { return (string)GetValue(UriProperty); }
set { SetValue(UriProperty, value); }
}
}
主页
//xmlns:local ="clr-namespace:App1"
<local:CustomWebView Uri="https://docs.microsoft.com/en-us/xamarin/ios/" HeightRequest="300"/>
自定义渲染器
[assembly: ExportRenderer(typeof(CustomWebView), typeof(HybridWebViewRenderer))]
namespace App1.iOS
{
class HybridWebViewRenderer : ViewRenderer<CustomWebView, WKWebView>
{
protected override void OnElementChanged(ElementChangedEventArgs<CustomWebView> e)
{
base.OnElementChanged(e);
if (Control == null)
{
var webView = new WKWebView(Frame, new WKWebViewConfiguration());
SetNativeControl(webView);
}
if (e.NewElement != null)
{
Control.LoadRequest(new NSUrlRequest(new NSUrl(Element.Uri)));
}
}
}
}
检测 Copy 事件的事件 将代码放在自定义渲染器或 AppDelegate 中。
NSNotificationCenter.DefaultCenter.AddObserver(UIPasteboard.ChangedNotification,(noti)=> {
var s = UIPasteboard.General.String;
MessagingCenter.Send<object,string>(this, "Hi",s);
},null);
推荐阅读
- angular - ngFor 内存不足
- mysql - MySQL根据日期获取最近的可用行
- json - 如何在有条件的情况下导出为 CSV?
- cloud - 使用 Virtualbox/VMware 创建内部计算云
- mysql - 将两个 SQL 结果合并到一个表中
- javascript - 如何可视化和导出 ee.Algorithms.TemporalSegmentation.Ccdc 的输出?
- python - 我可以从简单查询中获得什么性能:简单示例很慢
- python - 创建命令说嵌入到 discord.py
- r - 根据 R 中的另一列对一列求和,但跳过带有 NA 的行
- java - 应用程序崩溃与 android 材料芯片组与材料 1.3.0