11

there is a way to activate a control WebView Desktop mode and not Mobile mode?

<WebView x:name= "WebViewApp" ..../>
SMM
  • 201
  • 1
  • 9
  • The "credible and/or official sources" part above is a mistake. I just want it to work . . . – William Jockusch Jan 10 '17 at 17:37
  • Bounty to first person who can show how to load an html string from memory with a particular base url and th same appearance as desktop. – William Jockusch Jan 11 '17 at 12:36
  • @WilliamJockusch Did you read http://stackoverflow.com/questions/7585210/webview-add-local-css-file-to-an-html-page – Sir Rufo Jan 11 '17 at 16:55
  • That seems to be talking about an Android WebView. This question is about Windows.UI.Xaml.Controls.WebView. – William Jockusch Jan 11 '17 at 18:00
  • @WilliamJockusch that's a different problem and should be asked in its own question. It would help if you explained how it looked different. That said, you probably need to use NavigateToLocalStreamUriAsync to provide the related resources. See https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/XamlWebView for an official sample. – Rob Caplan - MSFT Jan 17 '17 at 11:40

2 Answers2

5

The WebView doesn't have an inherent desktop or mobile mode. Whether a site provides a mobile or desktop optimised site is generally based on the User Agent header. You can set this in a WebView by creating an HttpWebRequest with the agent you want and then navigating with WebView.NavigateWithHttpRequestMessage.

If you want to mimic a specific browser mode you can find the user agent it uses at several web sites.

string userAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; ARM; Trident/7.0; Touch; rv:11.0; WPDesktop) like Gecko"
HttpRequestMessage httpRequestMessage = new HttpRequestMessage(
    HttpMethod.Post, new Uri("http://whatsmyuseragent.com"));
httpRequestMessage.Headers.Append("User-Agent",userAgent);

myWebView.NavigateWithHttpRequestMessage(httpRequestMessage);
Rob Caplan - MSFT
  • 21,714
  • 3
  • 32
  • 54
  • 1
    How can I change the user agent to load local html by `myWebView.NavigateToLocalStreamUri(url, myResolver)` ? – Mostafiz Rahman Sep 04 '15 at 04:02
  • @mostafiz why you should? User-Agent is request header for Web server. It used by server to decide what content to give back. Since you have no web server and no request you have no User-Agent. – ad1Dima Jan 17 '17 at 09:01
-1

You can try adding a viewport meta tag with a "desktop" width to your <head>, like:

<meta name="viewport" content="width=1024">

or

<meta name="viewport" content="width=1024,initial-scale=1, maximum-scale=1, user-scalable=no">

Try this in order to set your user agent:~

webView.SetWebViewClient (new MyWebViewClient());

// ...

public class MyWebViewClient : WebViewClient
{
    public override bool ShouldOverrideUrlLoading(WebView view, string url)
    {
        view.Settings.UserAgentString = "your-user-agent-here";
        view.LoadUrl(url);
        return true;
    }
}

Yoav Aharoni
  • 2,672
  • 13
  • 18
  • I don't see any WebViewClient class, nor do I see a SetWebViewClient method on the relevant WebView. https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.webview.aspx – William Jockusch Jan 17 '17 at 09:22
  • Oh sorry, guess I read your question too quickly :) The Android one has one (Xamarin): https://developer.xamarin.com/api/member/Android.Webkit.WebView.SetWebViewClient/p/Android.Webkit.WebViewClient/ https://developer.xamarin.com/api/type/Android.Webkit.WebViewClient/ But regardless, try to add the width=1024 viewport tag and see if that helps. – Yoav Aharoni Jan 17 '17 at 10:01