在 Webview 渲染器中处理自定义事件

Handle custom events in Webview Renderer

我正在尝试处理来自自定义 webview 的事件,但该事件根本没有被触发,我将在此处放置一些代码

在我的 PCL 项目中 CustomWebview.cs

namespace TesteNovo
{
    public class CustomWebview : WebView
    {
        public EventHandler<int> Test;
    }
}

在我的 Android 项目中 CustomWebviewAndroid.cs

[assembly: ExportRenderer(typeof(Android.Webkit.WebView),typeof(CustomWebviewAndroid))]
namespace TesteNovo.Droid
{

    public class CustomWebviewAndroid : Android.Webkit.WebView
    {
        public CustomWebviewAndroid(Context context) : base(context)
        {
            var cl = new CustomWebViewClient();
            cl.ErroTeste += (a, b) => {
                var t = new CustomWebview();
                t.Test?.Invoke(this, b);

            };

            SetWebViewClient(cl);

        }

    }
}

在我的 Android 项目 CustomWebviewClient

namespace TesteNovo.Droid
{
    public class CustomWebViewClient : WebViewClient
    {


        public EventHandler<int> ErroTeste;

        public override void OnReceivedError(WebView view, IWebResourceRequest request, WebResourceError error)
        {
            ErroTeste?.Invoke(this, 404);
            base.OnReceivedError(view, request, error);

        }

        public override void OnReceivedHttpError(WebView view, IWebResourceRequest request, WebResourceResponse errorResponse)
        {
            base.OnReceivedHttpError(view, request, errorResponse);
            ErroTeste?.Invoke(this, 404);
        }



    }
}

在我的 PCL 项目中 Mainpage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:TesteNovo;assembly=TesteNovo"
             mc:Ignorable="d"
             x:Class="TesteNovo.MainPage">

    <StackLayout>
        <!-- Place new controls here -->
        <local:CustomWebview   x:Name="teste1" Source="https://www.sincor.77seg.com.br/" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" >

        </local:CustomWebview>

    </StackLayout>

</ContentPage>

在我的 PCL MainPage.cs

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        teste1.Test += async (a, b) => {
            await DisplayAlert(b.ToString(), "teste", "OK");

        };
    }
}

第一个目标是当我收到来自 http 请求的任何错误时触发测试事件,下一个目标是选择将触发测试事件的错误。

如果您需要更多代码或详细信息,请在评论中告诉我。

1.Display MainThread 中的警报:

public MainPage()
{
    InitializeComponent();

    teste1.Test += async (a, b) => {

        Device.BeginInvokeOnMainThread(async () => {
            await DisplayAlert(b.ToString(), "teste", "OK");
        });
    };
}

2.You 应该写 CustomWebview 的渲染器而不是 Android.Webkit.WebView:

[assembly: ExportRenderer(typeof(CustomWebview),typeof(CustomWebviewAndroid))]
namespace App81.Droid
{

    public class CustomWebviewAndroid : ViewRenderer<CustomWebview, Android.Webkit.WebView> {

        Context _context;

        public CustomWebviewAndroid(Context context) : base(context)
        {

            _context = context;

        }
        protected override void OnElementChanged(ElementChangedEventArgs<CustomWebview> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                if (Control == null)
                {
                    var webView = new Android.Webkit.WebView(_context);
                    webView.Settings.JavaScriptEnabled = true;

                    var cl = new CustomWebViewClient();
                    cl.ErroTeste += (a, b) => {
                        e.NewElement.Test?.Invoke(this, b);
                    };

                    webView.SetWebViewClient(cl);

                    SetNativeControl(webView);

                }

                Control.LoadUrl($"https://www.sincor.77seg.com.br/");
            }
        }
    }

    public class CustomWebViewClient : WebViewClient
    {


        public EventHandler<int> ErroTeste;

        public override void OnReceivedError(WebView view, IWebResourceRequest request, WebResourceError error)
        {
            ErroTeste?.Invoke(this, 404);
            base.OnReceivedError(view, request, error);

        }

        public override void OnReceivedHttpError(WebView view, IWebResourceRequest request, WebResourceResponse errorResponse)
        {
            base.OnReceivedHttpError(view, request, errorResponse);
            ErroTeste?.Invoke(this, 404);
        }
    }
}   

3.Another 方法是使用 messaging-center,消息中心可以在 xxx.Android 项目和共享项目之间传递数据。