我需要在 Xamarin WebView 中將 SetSupportMultipleWindows 設定為 false,因為我無法加載其他域名的頁面。我是 C# 和 Xamarin 的新手,所以我使用 YT 教程來構建我的 WebView 和 Splashscreen。我從網上嘗試了多種選擇,但無法自己解決。我唯一需要改變的是這個設定。
主頁.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"
x:Class="TruckAndTrailer.MainPage">
<WebView x:Name="webview"></WebView>
</ContentPage>
MainPage.xaml.cs
using Xamarin.Forms;
namespace TruckAndTrailer
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
webview.Source = "https://www.google.com/";
webview.Navigated = (o, s) =>{
webview.EvaluateJavaScriptAsync("document.getElementById('mk-footer').style.display = 'none';");
};
}
protected override bool OnBackButtonPressed()
{
if (webview.CanGoBack)
{
webview.GoBack();
return true;
}
return false;
}
}
}
MainActivity.cs
using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Content;
using Xamarin.Forms;
namespace TruckAndTrailer.Droid
{
[Activity(Theme = "@style/MainTheme", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
如果還需要什么,我會提供。
uj5u.com熱心網友回復:
如果你只是想設定這個設定,你可以像
hvaughan3 說的那樣使用自定義渲染器。
[assembly: ExportRenderer(typeof(Xamarin.Forms.WebView), typeof(ProjectTest.Droid.MyWebViewRender))]
namespace ProjectTest.Droid
{
public class MyWebViewRender : Xamarin.Forms.Platform.Android.WebViewRenderer
{
public MyWebViewRender(Context context) : base(context) { }
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged(e);
if(Control != null)
{
Control.Settings.SetSupportMultipleWindows(false);
}
}
}
}
使用上面的代碼在您的 android 部分中創建一個渲染。
更新
或者您想在瀏覽器中打開一些特殊鏈接,您可以檢查這種將導航事件添加到 webview 的案例來做到這一點。
此外,您還可以使用 costom 渲染器檢查打開外部鏈接的鏈接并覆寫該ShouldOverrideUrlLoading方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/522421.html
標籤:C#xamarinxamarin.forms网页浏览xamarin.android
下一篇:如何使彈出視頻回應于移動設備
