(Android & iOS) Xamarin - 如何从 WebView 获取 tapped/selected 文本或段落?
(Android & iOS) Xamarin - How to get tapped/selected text or paragraph from a WebView?
如何从 WebView 中获取选定的文本范围?是否有 selectedSomething 等 WebView 自定义事件?
--
若无解;
我可以从网络视图中复制 text/paragraph 吗?是否有用于复制的事件(也许是自定义事件)?
--
我很乐意提供任何帮助。
select离子会在我们 select 字符串后不久消失。该解决方案来自 Xamarin 论坛中的 ColeX。 selection 可以创建自定义渲染器并将默认的 webview 替换为 WKWebView。
(https://forums.xamarin.com/discussion/comment/396734)
CustomWebView
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)));
}
}
}
}
检测复制事件
将代码放在自定义渲染器或 AppDelegate 中。
NSNotificationCenter.DefaultCenter.AddObserver(UIPasteboard.ChangedNotification,(noti)=> {
var s = UIPasteboard.General.String;
MessagingCenter.Send<object,string>(this, "Hi",s);
},null);
如何从 WebView 中获取选定的文本范围?是否有 selectedSomething 等 WebView 自定义事件?
--
若无解;
我可以从网络视图中复制 text/paragraph 吗?是否有用于复制的事件(也许是自定义事件)?
--
我很乐意提供任何帮助。
select离子会在我们 select 字符串后不久消失。该解决方案来自 Xamarin 论坛中的 ColeX。 selection 可以创建自定义渲染器并将默认的 webview 替换为 WKWebView。
(https://forums.xamarin.com/discussion/comment/396734)
CustomWebView
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)));
}
}
}
}
检测复制事件 将代码放在自定义渲染器或 AppDelegate 中。
NSNotificationCenter.DefaultCenter.AddObserver(UIPasteboard.ChangedNotification,(noti)=> {
var s = UIPasteboard.General.String;
MessagingCenter.Send<object,string>(this, "Hi",s);
},null);