如何使用Xamarin将webview移动的设置为桌面模式

yqyhoc1h  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(250)

试过这个

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        webView.Source = "https://www.example.com/";
        webView.Navigating += WebView_Navigating;
    }

    private void WebView_Navigating(object sender, WebNavigatingEventArgs e)
    {
        // Set the User-Agent string to a desktop browser user agent
        webView.Eval("navigator.userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'");
    }
}

请帮助在更改到桌面模式使用Xamarin

nr7wwzry

nr7wwzry1#

在Android中,您必须实现自定义渲染器。
在Android文件夹中创建自定义渲染器(DesktopWebViewRenderer.cs):

[assembly: ExportRenderer(typeof(WebView), typeof(DesktopWebViewRenderer))]
namespace forms.Droid
{
    public class DesktopWebViewRenderer : WebViewRenderer
    {
        public DesktopWebViewRenderer(Context context): base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
            base.OnElementChanged(e);

            Control.Settings.UserAgentString
                = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";

        }
    }
}

相关问题