c# - 如何获取 webview 当前持有的网页标题?
问题描述
我正在尝试获取我的 webview 的标题,所以我可以将它作为字符串存储在我的数据库中。但我找不到这样做的方法。
我尝试使用mwebview.Title
,但有时它给我的结果与 URL 相同。
解决方案
这是我在自定义 WebViewClient 中使用 OnPageFinished 覆盖的完整示例。
WebViewCustomActivity.cs
using System;
using Android.App;
using Android.OS;
using Android.Webkit;
namespace XamdroidMaster.Activities {
[Activity(Label = "Custom WebViewClient", MainLauncher = true)]
public class WebViewCustomActivity : Activity {
protected override void OnCreate(Bundle savedInstanceState) {
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.WebView);
WebView wv = FindViewById<WebView>(Resource.Id.webviewMain);
CustomWebViewClient customWebViewClient = new CustomWebViewClient();
customWebViewClient.OnPageLoaded += CustomWebViewClient_OnPageLoaded;
wv.SetWebViewClient(customWebViewClient);
wv.LoadUrl("https://www.stackoverflow.com");
}
private void CustomWebViewClient_OnPageLoaded(object sender, string sTitle) {
Android.Util.Log.Info("MyApp", $"OnPageLoaded Fired - Page Title = {sTitle}");
}
}
public class CustomWebViewClient : WebViewClient {
public event EventHandler<string> OnPageLoaded;
public override void OnPageFinished(WebView view, string url) {
OnPageLoaded?.Invoke(this, view.Title);
}
}
}
WebView.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/WebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<WebView
android:id="@+id/webviewMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" />
</LinearLayout>
推荐阅读
- php - 在现有的php+laravel+mysql项目中实现nodejs
- c# - 从 X509CertificateStore 读取未处理的异常
- apache-camel - 骆驼路线中未打印的日志消息
- c# - 如何使用 Linq 在带有字符串的集合上加入带有字符串 [] 的 MongoDB 集合
- opencv - 内核在重新启动 jupyter notebook 时死机
- javascript - 如何修复错误“错误类型错误:无法读取未定义的属性'0'”
- reactjs - 如何在 Jsdoc 上保留源代码文件夹结构
- node.js - 已退出(nodejs)应用程序的谷歌应用引擎日志在哪里
- javascript - 使用 Javascript 禁用表单按钮和字段
- android - Android 发送广播到其他模块