La détection de la version du navigateur dans WPF
-
25-09-2019 - |
Question
Est-il possible de savoir dans quelle application hébergée version du navigateur d'un navigateur (XBAP) fonctionne (par exemple. IE6, IE7 ou IE8)? Je veux savoir la version du navigateur à partir du XBAP.
La solution
Avec l'aide d'un forum Microsoft m'a conduit dans une direction qui fonctionne enfin. Ci-dessous une preuve de concept en 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;
}
Autres conseils
Je suppose que vous voulez dire Silverlight plutôt que WPF? (Ils sont des technologies distinctes, mais similaires).
Jetez un oeil à la page System.Windows.Browser.BrowserInformation
classe
Plus précisément
System.Windows.Browser.BrowserInformation.BrowserVersion
A partir de la page MSDN ci-dessus:
using System;
en utilisant System.Windows.Controls; en utilisant 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";
}
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow