且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以将JavaScript和CSS注入Xamarin Form WebView吗?

更新时间:2022-12-11 11:34:18

WebView 提供了 EvaluateJavaScriptAsync 方法(请参见

The WebView provides an EvaluateJavaScriptAsync method (see here), that you can use to execute JS.

例如,您可以订阅 Navigated 事件(请参阅

You could for example subscribe to the Navigated event (see here)

<WebView VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Source="<SOME_URL_HERE>" Navigated="WebViewOnNavigated" />

,并且当 WebView 导航到后,您可以从后面的代码中执行JS

and when the WebView has navigated to, you can execute your JS from code behind

public async void WebViewOnNavigated(object sender, WebNavigatedEventArgs args)
{
    var webView = sender as WebView; // ommitting the null check, since nobody else will call this method
    await webView.EvaluateJavaScriptAsync("<Your JS goes here>");
}