作者:遇见你_天意_384 | 来源:互联网 | 2023-09-03 22:18
我正在尝试通过动态创建WebView
并加载源URL来自动提取一个网站数据。我希望不向用户显示Page
和WebView
。不幸的是,这些事件似乎不起作用。任何帮助表示赞赏。
创建ContentPage
并在设计时添加XAML很好。
修改1:
感谢Paul为我提供详细的事件模型方面的帮助。
不幸的是,我的要求是不向用户显示页面和Web视图。如果未将其添加到可视树中,则似乎不会触发webview事件:-(。
也许我应该对我要实现的目标进行更多解释。从“ xamarin表单” /“ xamarin android”平台应用程序/服务中自动进行网站爬网的最佳方法是什么?(我在这里添加了服务,因为我想在后台自动进行此操作)我开始使用webview探索因为它使用本机浏览器并具有相当好的Javascript支持,甚至允许动态Javascript注入。有没有其他选择,例如无头浏览器,它可以实现相同功能,包括Javascript注入?我仍然更喜欢webview,因为它具有灵活性,希望我们可以使其以某种方式工作。
这就是我想要做的:
ContentPage cp = new ContentPage();
cp.Title = "My Page";
StackLayout sl = new StackLayout();
var webView = new WebView();
sl.Children.Add(webView);
cp.COntent= sl;
webView.Navigating += WebView_Navigating;
webView.Navigated += WebView_Navigated;
webView.Source = "https://www.google.com/";//just a sample i tried multiple urls including http
private async void WebView_Navigating(object sender,WebNavigatingEventArgs e)
{
//event not firing!
}
private async void WebView_Navigated(object sender,WebNavigatedEventArgs e)
{
//event not firing!
}
我不知道您将要做什么,但是如果您希望不显示Web视图,则可以使用 isVisible 属性。
您可以在一个显示页面中动态创建Webview。
例如,这是一个显示页面:
public partial class HyWeb : ContentPage
{
public HyWeb()
{
InitializeComponent();
}
private void Button_Clicked(object sender,EventArgs e)
{
WebView webView = new WebView();
webView.WidthRequest = 500;
webView.HeightRequest = 500;
webView.IsVisible = false;
webView.Source = "https://www.google.com";
webView.Navigated += WebView_Navigated;
webView.Navigating += WebView_Navigating;
Stl.Children.Add(webView);
}
private void WebView_Navigated(object sender,WebNavigatedEventArgs e)
{
Console.WriteLine("navigated");
}
private void WebView_Navigating(object sender,WebNavigatingEventArgs e)
{
Console.WriteLine("navigating");
}
}
第xaml页:
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"
mc:Ignorable="d"
xmlns:local="clr-namespace:App18"
x:Class="App18.HyWeb">
当我单击按钮时,它将动态创建Web视图,但是Web视图将不会显示给用户
,
从实际的角度来看,朱宝o(Leo Zhu)写的是正确的,但让我详细介绍一下。
创建WebView
的方式不会将其添加到应用程序的可视树中,因为页面已创建但从未显示过。
我创建了一个小示例,您可以在附件中找到该示例,在其中创建了一个页面,该页面包含一个向该页面添加WebView
的按钮。无论如何,单击按钮时不会创建此WebView
,而是在页面的构造函数中创建。 Navigated
事件调用一种方法,该方法会在事件发生时进行记录。
此外,我创建了一个自定义渲染器,以记录WebView
的渲染器以及创建本机控件的时间。
我的调试消息按以下顺序发生
[0:] WebView created
[0:] Button clicked
[0:] Creating renderer
[0:] Creating native control
[0:] WebView navigated
这样,您可以看到-在构造函数中创建Xamarin.Forms视图时-实例化了本地渲染器(负责创建本地视图,而后者又负责加载网站)在之后单击按钮(即,在将视图添加到应用程序的可视树之后)。然后,渲染器创建用于加载网站的本机控件。最后,在加载网站之后,将引发Navigated
事件。如果将WebView
添加到页面并在按下按钮后显示此页面,则基本上相同。
附件(代码)
XAML:
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"
mc:Ignorable="d"
x:Class="SOTest20191112.MainPage">
后面的代码:
namespace SOTest20191112
{
using System;
using System.ComponentModel;
using System.Diagnostics;
using Xamarin.Forms;
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
private WebView _webView;
public MainPage()
{
InitializeComponent();
this._webView = new WebView
{
Source = new UrlWebViewSource()
{
Url = "http://www.google.de"
}
};
_webView.Navigated += WebViewOnNavigated;
Debug.WriteLine("WebView created");
}
private void WebViewOnNavigated(object sender,WebNavigatedEventArgs e)
{
Debug.WriteLine("WebView navigated");
}
private void Button_OnClicked(object sender,EventArgs e)
{
Debug.WriteLine("Button clicked");
if (!this.StackLayout.Children.Contains(this._webView))
{
this.StackLayout.Children.Add(this._webView);
}
}
}
}
自定义渲染器
using SOTest20191112.Droid;
using Xamarin.Forms;
[assembly: ExportRenderer(typeof(WebView),typeof(WebViewRenderer))]
namespace SOTest20191112.Droid
{
using Android.Content;
using Android.Webkit;
class WebViewRenderer : Xamarin.Forms.Platform.Android.WebViewRenderer
{
public WebViewRenderer(Context context)
: base(context)
{
System.Diagnostics.Debug.WriteLine("Creating renderer");
}
protected override WebView CreateNativeControl()
{
System.Diagnostics.Debug.WriteLine("Creating native control");
return base.CreateNativeControl();
}
}
}