【发布时间】: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