【问题标题】:Detection of browser version in WPF在 WPF 中检测浏览器版本
【发布时间】:2010-04-08 10:19:44
【问题描述】:

是否可以查明浏览器托管应用程序 (XBAP) 在哪个浏览器版本中运行(例如 IE6、IE7 或 IE8)?我想从 XBAP 中找出浏览器版本。

【问题讨论】:

  • @Rob:你遇到过这个需求吗?如果有,是什么?
  • 对于我们的应用程序,我们需要有一种方法来报告我们的应用程序在哪个浏览器版本中运行。该报告是出于支持目的而生成的,并包含各种系统信息。不幸的是,浏览器版本确实会影响应用程序的行为。

标签: wpf internet-explorer client xbap version-detection


【解决方案1】:

在微软论坛的一些帮助下,我被引导到了一个最终可行的方向。下面是 C++.NET 中的概念证明 (.

using namespace System::Windows::Forms;

[STAThread]
String^ GetBrowserVersion() {
   String^ strResult = String::Empty;
   WebBrowser^ wb = gcnew WebBrowser();            
   String^ strJS = "<SCRIPT>function GetUserAgent() { return navigator.userAgent; }</SCRIPT>";
   wb->DocumentStream = gcnew MemoryStream( ASCIIEncoding::UTF8->GetBytes(strJS) );            
   while ( wb->ReadyState != WebBrowserReadyState::Complete ) {
      Application::DoEvents();
   }
   String^ strUserAgent = (String^)wb->Document->InvokeScript("GetUserAgent");
   wb->DocumentStream->Close();
   String^ strBrowserName = String::Empty;
   int i = -1;
   if ( ( i = strUserAgent->IndexOf( "MSIE" ) ) >= 0 ) {          
      strBrowserName = "Internet Explorer";
   } else if ( ( i = strUserAgent->IndexOf( "Opera" ) ) >= 0 ) {
      strBrowserName = "Opera";
   } else if ( ( i = strUserAgent->IndexOf( "Chrome" ) ) >= 0 ) {
      strBrowserName = "Chrome";
   } else if ( ( i = strUserAgent->IndexOf( "FireFox" ) ) >= 0 ) {
      strBrowserName = "FireFox";
   }
   if ( i >= 0 ) {
      int iStart = i + 5;
      int iLength = strUserAgent->IndexOf( ';', iStart ) - iStart;
      strResult = strBrowserName + " " + strUserAgent->Substring( iStart, iLength );
   }
   return strResult;
}

【讨论】:

  • 好主意,谢谢!在这里使用 C# 和 WPF。我的代码中有一个 WebBrowser 控件,看在上帝的份上,我找不到从哪里获得浏览器的版本。我所做的是使用外部 html 页面(与我的 .exe 相同的位置),这样加载它: WebBrowserMain.Navigate("pack://siteoforigin:,,,/useragent.html");在 useragent.html 中,通过收集用户代理信息的 javascript 在我的程序中调用了一个方法。非常感谢!
【解决方案2】:

我想你的意思是 Silverlight 而不是 WPF? (它们是独立的技术,虽然相似)。

看看System.Windows.Browser.BrowserInformation Class

具体

System.Windows.Browser.BrowserInformation.BrowserVersion

来自上面的 MSDN 页面:

using System;

使用 System.Windows.Controls; 使用 System.Windows.Browser;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
     outputBlock.Text +=
       "\nSilverlight can provide browser information:\n"
     + "\nBrowser Name = " + HtmlPage.BrowserInformation.Name
     + "\nBrowser Version = " + 
           HtmlPage.BrowserInformation.BrowserVersion.ToString()
     + "\nUserAgent = " + HtmlPage.BrowserInformation.UserAgent
     + "\nPlatform = " + HtmlPage.BrowserInformation.Platform
     + "\nCookiesEnabled = " + 
           HtmlPage.BrowserInformation.CookiesEnabled.ToString() + "\n";

   }
}

【讨论】:

  • XBAP(XAML 浏览器应用程序)——是可以在浏览器中托管的 WPF 应用程序。 msdn.microsoft.com/en-us/library/aa970060.aspx
  • 不,我的意思是确实是 XAML 浏览器应用程序。我认为使用您的解决方案会引入对 Silverlight 的新依赖项。不管怎么说,还是要谢谢你!我在 Microsoft 论坛的帮助下找到了另一个解决方案,请参阅我对自己问题的回复。
【解决方案3】:
int BrowserVer;
using (var wb = new System.Windows.Forms.WebBrowser())
{
  BrowserVer = wb.Version.Major;
}

【讨论】:

  • 添加更多细节来解释为什么以及如何回答这个问题。
猜你喜欢
  • 1970-01-01
  • 2016-03-21
  • 1970-01-01
  • 1970-01-01
  • 2014-12-28
  • 2012-05-11
  • 2011-08-20
  • 1970-01-01
  • 2011-03-14
相关资源
最近更新 更多