简体   繁体   中英

Xamarin forms custom WebViewRender on Android

I'm trying to add some headers to each request in WebView controller. I'm using a custom web renderer to add the headers. It's working fine on iOS but just adding the renderer class in Android causes an exception.

The code looks like this

In the PCL, I inherited the WebView control to add some custom properties

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; }
}

In the android project, I just created a blank render as follows

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

I got a generic exception whenever I tried to launch the page that contains the AuthWebView control

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

Is there any thing I missed here that can be causing the exception to happen ?

The problem seems related to an older version of Xamarin.Forms package and it just solved when i updated the nuget package to the latest version!

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM