2017-02-18 1 views
0

В следующем коде используется CSS для записи таблицы с чередующимися цветами строк в html-файл. После записи на диск при непосредственном просмотре html-файла, например. Firefox, Chrome, IE и т. Д., Видны чередующиеся цвета строк. Однако в VS2015 после загрузки файла html в элемент управления WebBrowser1 переменные цвета строк не видны.VS 2015 - элемент управления WebBrowser не отображает цвета строк чередующихся таблиц в CSS

Я проверил другие сообщения на SO и в других местах, и очевидно, что WebBrowser просто обертывает IE, и для преодоления проблем с WebBrowser существуют обходные пути, связанные с установкой сторонних открытых источников. Однако, что относительно простого решения этой проблемы от MS? Это похоже на недоразвитую проблему для MS и VS2015, так как подразумевает, что существующие элементы управления, связанные с VS2015, имеют проблемы с CSS. Любые решения на базе MS для этого?

Dim sw As New StreamWriter(filename) 
sw.WriteLine("<!DOCTYPE html><html><body>") 
sw.WriteLine("<style type=" & """" & "text/css" & """" & ">") 
sw.WriteLine("table.gridtable {") 
sw.WriteLine(" font-family: verdana,arial,sans-serif;") 
sw.WriteLine(" font-size:12px;") 
sw.WriteLine(" color: #333333;") 
sw.WriteLine(" border-width: 1px;") 
sw.WriteLine(" border-color: #666666;") 
sw.WriteLine(" border-collapse: collapse;") 
sw.WriteLine("}") 
sw.WriteLine("table.gridtable th {") 
sw.WriteLine(" border-width: 1px;") 
sw.WriteLine(" padding: 8px;") 
sw.WriteLine(" border-style: solid;") 
sw.WriteLine(" border-color: #666666;") 
sw.WriteLine(" background-color: #dedede;") 
sw.WriteLine("}") 
sw.WriteLine("table.gridtable tr {") 
sw.WriteLine("background-color: #ffffff;") 
sw.WriteLine("}") 
sw.WriteLine("table.gridtable tr:nth-child(even) {") 
sw.WriteLine("background-color: #fdf5e6;") 
sw.WriteLine("}") 
sw.WriteLine("table.gridtable td {") 
sw.WriteLine(" border-width: 1px;") 
sw.WriteLine(" padding: 8px;") 
sw.WriteLine(" border-style: solid;") 
sw.WriteLine(" border-color: #666666;") 
sw.WriteLine("}") 
sw.WriteLine("</style>") 

sw.WriteLine("<table class=" & """" & "gridtable" & """" & " Align=center>") 
sw.WriteLine("<tr>") 
sw.Write("<th>" & "Output" & "</th>") 
sw.Write("<th>" & "Output" & "</th>") 
sw.Write("<th>" & "Output" & "</th>") 
sw.Write("<th>" & "Output" & "</th>") 
sw.WriteLine("</tr>") 
sw.WriteLine("<tr>") 
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>") 
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>") 
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>") 
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>") 
sw.WriteLine("</tr>") 
sw.WriteLine("<tr>") 
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>") 
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>") 
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>") 
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>") 
sw.WriteLine("</tr>") 
sw.WriteLine("<tr>") 
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>") 
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>") 
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>") 
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>") 
sw.WriteLine("</tr>") 
sw.WriteLine("<tr>") 
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>") 
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>") 
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>") 
sw.WriteLine("<td align=""center""><font color=" & """" & "#000000" & """" & ">Hello</font></td>") 
sw.WriteLine("</tr>") 
sw.WriteLine("</table>") 
sw.WriteLine("</body></html>") 
sw.dispose 
+0

ознакомьтесь с этой страницей http://stackoverflow.com/questions/3822537/wpf-webbrowser-control-what-browser-does-it-use –

+0

и https://social.msdn.microsoft.com/Forums/ VStudio/EN-US/6abee729-ac0b-47c5-9603-d1b3050618af/WebBrowser-не-применение-CSS-правильно? форум = МОФ –

ответ

0

РЕШЕНИЕ: чтобы помочь другим, я преобразовал C# решение, опубликованным на сайте Configuring the emulation mode of an Internet Explorer WebBrowser control, by Richard James Moss в VB.NET.

Сначала добавьте следующие импорта в независимый модуль (не, например, Form1):

Imports Microsoft.Win32 
Imports System.Security 

Затем добавьте следующий класс в модуль:

Public Class InternetExplorerBrowserEmulation 
    Public Enum BrowserEmulationVersion 
     [Default] = 0 
     Version7 = 7000 
     Version8 = 8000 
     Version8Standards = 8888 
     Version9 = 9000 
     Version9Standards = 9999 
     Version10 = 10000 
     Version10Standards = 10001 
     Version11 = 11000 
     Version11Edge = 11001 
    End Enum 
    Private Const BrowserEmulationKey As String = InternetExplorerRootKey & "\Main\FeatureControl\FEATURE_BROWSER_EMULATION" 
    Public Function GetBrowserEmulationVersion() As BrowserEmulationVersion 
     Dim result As BrowserEmulationVersion 

     result = BrowserEmulationVersion.Default 

     Try 
      Dim key As RegistryKey 

      key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, True) 
      If key IsNot Nothing Then 
       Dim programName As String 
       Dim value As Object 

       programName = Path.GetFileName(Environment.GetCommandLineArgs()(0)) 
       value = key.GetValue(programName, Nothing) 

       If value IsNot Nothing Then 
        result = CType(Convert.ToInt32(value), BrowserEmulationVersion) 
       End If 
      End If 
     Catch e1 As SecurityException 
      ' The user does not have the permissions required to read from the registry key. 
     Catch e2 As UnauthorizedAccessException 
      ' The user does not have the necessary registry rights. 
     End Try 

     Return result 
    End Function 

    Public Function IsBrowserEmulationSet() As Boolean 
     Return GetBrowserEmulationVersion() <> BrowserEmulationVersion.Default 
    End Function 

    Public Function SetBrowserEmulationVersion(ByVal browserEmulationVersion As BrowserEmulationVersion) As Boolean 
     Dim result As Boolean 

     result = False 

     Try 
      Dim key As RegistryKey 

      key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, True) 

      If key IsNot Nothing Then 
       Dim programName As String 

       programName = Path.GetFileName(Environment.GetCommandLineArgs()(0)) 

       If browserEmulationVersion <> BrowserEmulationVersion.Default Then 
        ' if it's a valid value, update or create the value 
        key.SetValue(programName, CInt(Fix(browserEmulationVersion)), RegistryValueKind.DWord) 
       Else 
        ' otherwise, remove the existing value 
        key.DeleteValue(programName, False) 
       End If 

       result = True 
      End If 
     Catch e1 As SecurityException 
      ' The user does not have the permissions required to read from the registry key. 
     Catch e2 As UnauthorizedAccessException 
      ' The user does not have the necessary registry rights. 
     End Try 

     Return result 
    End Function 

    Public Function SetBrowserEmulationVersion() As Boolean 
     Dim ieVersion As Integer 
     Dim emulationCode As BrowserEmulationVersion 

     ieVersion = GetInternetExplorerMajorVersion() 

     If ieVersion >= 11 Then 
      emulationCode = BrowserEmulationVersion.Version11 
     Else 
      Select Case ieVersion 
       Case 10 
        emulationCode = BrowserEmulationVersion.Version10 
       Case 9 
        emulationCode = BrowserEmulationVersion.Version9 
       Case 8 
        emulationCode = BrowserEmulationVersion.Version8 
       Case Else 
        emulationCode = BrowserEmulationVersion.Version7 
      End Select 
     End If 

     Return SetBrowserEmulationVersion(emulationCode) 
    End Function 

    Private Const InternetExplorerRootKey As String = "Software\Microsoft\Internet Explorer" 

    Public Function GetInternetExplorerMajorVersion() As Integer 
     Dim result As Integer = Nothing 

     result = 0 

     Try 
      Dim key As RegistryKey 

      key = Registry.LocalMachine.OpenSubKey(InternetExplorerRootKey) 

      If key IsNot Nothing Then 
       Dim value As Object 

       value = If(key.GetValue("svcVersion", Nothing), key.GetValue("Version", Nothing)) 

       If value IsNot Nothing Then 
        Dim version As String 
        Dim separator As Integer 

        version = value.ToString() 
        separator = version.IndexOf("."c) 
        If separator <> -1 Then 
         Integer.TryParse(version.Substring(0, separator), result) 
        End If 
       End If 
      End If 
     Catch e1 As SecurityException 
      ' The user does not have the permissions required to read from the registry key. 
     Catch e2 As UnauthorizedAccessException 
      ' The user does not have the necessary registry rights. 
     End Try 

     Return result 
    End Function 
End Class 

Наконец, в процедуре Form1_Load, добавьте следующий код:

Dim IEemulation As New InternetExplorerBrowserEmulation 
If Not IEemulation.IsBrowserEmulationSet() Then 
    IEemulation.SetBrowserEmulationVersion() 
End If 

При запуске программы выполняется эмуляция IE, поэтому t все возможности CSS наблюдаются в элементе управления Webbrowser.

Смежные вопросы