Home > Back-end >  Need to implement SetSupportMultipleWindows on Xamarin Webview app
Need to implement SetSupportMultipleWindows on Xamarin Webview app

Time:10-28

I need to set SetSupportMultipleWindows to false in Xamarin WebView, because I'm unable to load pages with other domain name. I'm newbie in C# and Xamarin, so I used YT tutorials to build my WebView and Splashscreen. I tried multiple options from web, but can't solve it by my own. Only thing i need to change is this setting.

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);
        }
    }
}

If anything more is needed I will provide it.

CodePudding user response:

If you just want to set this setting, you can use the custom renderer as
hvaughan3 said.

[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);
        }
    }
}
}

Create a render in your android part with the code above.

Update

Or you want to open some special link in the browser you can check this case which added the navigating event to the webview to do that.

In addtion, you can also check this link which open external link by using the costom renderer and override the ShouldOverrideUrlLoading method.

  • Related