我正在尝试处理来自自定义Web视图的事件,但是该事件根本没有触发,我将在此处放置一些代码
在我的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事件的错误。
如果您需要更多代码或详细信息,请在评论中告诉我。
答案 0 :(得分: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,消息传递中心可以在xxx.Android项目和共享项目之间传递数据。