【问题标题】:there is a way to activate a control WebView Desktop mode and not Mobile mode?有没有办法激活控件 WebView 桌面模式而不是移动模式?
【发布时间】:2015-02-16 20:40:11
【问题描述】:

有没有办法激活控件 WebView 桌面模式而不是移动模式?

<WebView x:name= "WebViewApp" ..../>

【问题讨论】:

  • 上面的“可信和/或官方来源”部分是错误的。我只是想让它工作。 . .
  • 赏金第一个可以展示如何从内存中加载具有特定基本 url 并且外观与桌面相同的 html 字符串的人。
  • @WilliamJockusch 你读过stackoverflow.com/questions/7585210/…
  • 这似乎是在谈论一个Android WebView。这个问题是关于 Windows.UI.Xaml.Controls.WebView。
  • @WilliamJockusch 这是一个不同的问题,应该在自己的问题中提出。如果您解释它的外观有何不同,将会有所帮助。也就是说,您可能需要使用 NavigateToLocalStreamUriAsync 来提供相关资源。官方示例见github.com/Microsoft/Windows-universal-samples/tree/master/…

标签: c# xaml windows-phone-8.1


【解决方案1】:

WebView 没有固有的桌面或移动模式。站点是否提供移动或桌面优化站点通常基于用户代理标头。您可以通过使用所需代理创建HttpWebRequest,然后使用 WebView.NavigateWithHttpRequestMessage 导航,在 WebView 中进行设置。

如果您想模仿特定的浏览器模式,您可以在多个网站上找到它使用的用户代理。

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

【讨论】:

  • 如何更改用户代理以通过myWebView.NavigateToLocalStreamUri(url, myResolver) 加载本地html?
  • @mostafiz 为什么你应该这样做? User-Agent 是 Web 服务器的请求头。服务器使用它来决定返回什么内容。由于您没有 Web 服务器,也没有请求,因此您没有 User-Agent。
【解决方案2】:

您可以尝试将具有“桌面”宽度的视口元标记添加到您的&lt;head&gt;,例如:

&lt;meta name="viewport" content="width=1024"&gt;

&lt;meta name="viewport" content="width=1024,initial-scale=1, maximum-scale=1, user-scalable=no"&gt;

试试这个来设置你的用户代理:~

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

【讨论】:

猜你喜欢
  • 2018-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 2022-07-22
  • 1970-01-01
相关资源
最近更新 更多