在 WP8 WebBrowser 控件中显示 m.bing.com [英] Showing m.bing.com in the WP8 WebBrowser control

查看:14
本文介绍了在 WP8 WebBrowser 控件中显示 m.bing.com的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将 bing.com 加载到 Windows Phone 8 上的 WebBrowser 控件中时遇到问题.似乎这样做会启动 WP8 搜索应用程序(与按手机上的搜索按钮相同).问题是,一旦您单击该搜索应用程序中的结果,它并不会将您带回原始应用程序 - 它会转到 IE 以显示结果.这对我不起作用,似乎是 WebBrowser 行为中的一个巨大缺陷 (IMO).

I'm having a problem getting bing.com to load in a WebBrowser control on Windows Phone 8. It seems that doing that will launch the WP8 Search App (same as pressing the Search button on the phone). The problem is, once you click a result in that search app, it doesn't take you back to your original app - it goes to IE to show the result. This isn't going to work for me and seems to be a massive flaw (IMO) in the WebBrowser behavior.

似乎有一些应用程序可以在不启动手机搜索应用程序的情况下成功显示 bing.com - 例如 Image Downloader Free.还有一个,但我不记得是什么了...

There does seem to be a few apps out there that have succeeded in being able to show bing.com without launching the phone's search app - for example Image Downloader Free. There was another one, but I can't remember what it was...

经过一些研究,我发现在访问 bing.com 时 WebBrowser_Navigating 事件被触发了 3 次:首先请求用户输入的 URL (www.bing.com),然后它被重定向到 http://wp.m.bing.com/?mid=10006,然后重定向到 bing://home/?mid=10006.

After some research, I've found that the WebBrowser_Navigating event gets fired 3 times when going to bing.com: first request to the user-entered URL (www.bing.com), it then gets redirected to http://wp.m.bing.com/?mid=10006, then it redirects to bing://home/?mid=10006.

阻止它转发到 Bing 搜索应用程序非常简单,只需将其添加到 Navigating 事件中即可:

Preventing it from forwarding to the Bing search app is quite simple, just add this to the Navigating event:

e.Cancel = (e.Uri.Scheme == "bing");

问题是,它然后只显示 Bing 搜索页面占位符,上面写着Bing 搜索",并有一个链接,上面写着返回 Bing 搜索",它什么都不做(它通常会重新启动 Bing 搜索应用程序).

The problem is, it then only shows the Bing search page place holder which says "Bing Search" and has a link that says "Back to Bing search" which does nothing (it would typically relaunch the Bing Search app).

我有一些想法,但我不确定它们的可行性.

I have a few thoughts, but I'm not sure how feasible they are.

  • 在 WP8 WebBrowser 控件中,是否可以伪造用户代理?
  • 能否删除或添加 WebBrowser.Uri.Flags 属性中的一项以影响 Bing.com 处理请求的方式?
  • 如果这些都不起作用,我可以简单地在我的 Web 服务器上创建一个虚拟页面,将所有 bing.com 请求重定向到它,并让它使用卡片编码的用户代理抓取 m.bing.com 的首页.不过,我真的很想避免必须执行此选项.从最终用户的角度来看,他们永远不会知道,但我只是添加了一个全新的开销、维护和资源层.

如果您有兴趣,请附上 WebBrowser.Navigating 事件中发生的 3 个请求之间 EventArgs 对象的差异:

If you're interested, attached are the diff's for the EventArgs object between the 3 requests that occur in the WebBrowser.Navigating event:

请求 1 (bing.com) -> 请求 2(转发到 wp.m.bing.com/?mid=10006)

请求 2(转发到 wp.m.bing.com/?mid=10006)-> 请求 3(转发到 bing://home/?mid=10006)

tl;dr 有谁知道防止 www.bing.com 导致搜索应用在我的应用程序的 WebBrowser 控件中启动的方法吗?

谢谢!

推荐答案

我不知道是否有更好的方法来处理这个问题,但我找到了解决方案.单击后退按钮时,我还没有让它完美工作,所以如果/当我找到更可靠的解决方案时,我会更新我的答案.我还是觉得这是WP8中WebBrowser控件的一大缺陷.

I don't know if there's a better way to handle this, but I found a solution. I haven't gotten it to work perfectly when the back button is clicked, so I will update my answer if/when I found a more solid solution. I still think this is a big flaw in the WebBrowser control in WP8.

代码如下:

private bool _customHeaderRequest = false;

private void MainBrowser_Navigating(object sender, NavigatingEventArgs e)
{
    string host = e.Uri.Host.ToLowerInvariant().Trim();

    if ((host == "bing.com" || host.EndsWith(".bing.com")) && !_customHeaderRequest)
    {
        e.Cancel = true;

        Dispatcher.BeginInvoke(() =>
            MainBrowser.Navigate(e.Uri, null,
                "User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; NOKIA; Lumia 710)
"));

        _customHeaderRequest = true;
        return;
    }

    _customHeaderRequest = false;
}

private void MainBrowser_Navigated(object sender, NavigationEventArgs e)
{
    _customHeaderRequest = false;
}

这篇关于在 WP8 WebBrowser 控件中显示 m.bing.com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆