需要在Xamarin Webview应用程序上实现SetSupportMultipleWindows

new9mtju  于 2022-12-07  发布在  Windows
关注(0)|答案(1)|浏览(134)

我需要在Xamarin WebView中将SetSupportMultipleWindows设置为false,因为我无法加载其他域名的页面。我是C#和Xamarin的新手,所以我使用YT教程来构建我的WebView和闪屏。我尝试了Web上的多个选项,但无法自行解决。我唯一需要更改的是此设置。
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"
             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);
        }
    }
}

如果还需要什么,我会提供。

fv2wmkja

fv2wmkja1#

如果只想设置此设置,则可以将自定义渲染器用作
hvaughan说。

[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部件中创建一个渲染。

    • 更新**

或者你想在浏览器中打开一些特殊的链接,你可以选中this case,它将导航事件添加到webview来完成这个任务。
此外,您还可以使用定制渲染器检查打开外部链接的this link,并覆盖ShouldOverrideUrlLoading方法。

相关问题