【问题标题】:Handle custom events in Webview Renderer在 Webview Renderer 中处理自定义事件
【发布时间】:2020-03-25 06:13:34
【问题描述】:

我正在尝试处理来自自定义 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 请求的任何错误时触发 Test 事件,下一个目标是选择触发 Test 事件的错误。

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

【问题讨论】:

    标签: c# xamarin.forms webview


    【解决方案1】:

    1.在MainThread中显示警报:

    public MainPage()
    {
        InitializeComponent();
    
        teste1.Test += async (a, b) => {
    
            Device.BeginInvokeOnMainThread(async () => {
                await DisplayAlert(b.ToString(), "teste", "OK");
            });
        };
    }
    

    2.你应该写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.另一种实现方式是使用messaging-center,messaging-center可以在xxx.Android项目和共享项目之间传递数据。

    【讨论】:

    • 兄弟,工作得很好,顺便说一句,你能给我看看 iOS 渲染吗?
    • @GuilhermeNimer 看看这个answer;
    猜你喜欢
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多