Xamarin 在 Android 上形成自定义 WebViewRender

Xamarin forms custom WebViewRender on Android

我正在尝试向 WebView 控制器中的每个请求添加一些 headers。我正在使用自定义 Web 渲染器来添加 headers。它在 iOS 上运行良好,但仅在 Android 中添加渲染器 class 会导致异常。

代码如下所示

在PCL中,我继承了WebView控件添加了一些自定义属性

public class AuthWebView : WebView
{
    public static readonly BindableProperty UrlProperty =
         BindableProperty.Create("Url", typeof(string), typeof(AuthWebView), default(string));

    public string Url
    {
        get => (string)GetValue(UrlProperty);
        set => SetValue(UrlProperty, value);
    }

    public string UserToken { get; set; }
}

在android项目中,我刚刚创建了一个空白渲染如下

[assembly: ExportRenderer(typeof(AuthWebView), typeof(TestRenderer))]
namespace Reshare.Droid
{
    public class TestRenderer : WebViewRenderer
    {
    }
}

每当我尝试启动包含 AuthWebView 控件的页面时,我都会遇到一般异常

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target invocation

我在这里遗漏了什么可能导致异常发生的事情吗?

这个问题似乎与旧版本的 Xamarin.Forms 包有关,当我将 nuget 包更新到最新版本时它就解决了!

just for reference if anyone ever faced this issue, i was using Xamarin.Forms.2.4.0.38779 and updated now to version Xamarin.Forms.2.5.0.91635 and I was using the approach detailed here https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/hybridwebview/