2017-02-18 28 views
0

下面的代碼將使用CSS來將具有交替行顏色的表格寫入html文件。在寫入磁盤之後,當用例如直接查看html文件時Firefox,Chrome,IE等,交替的行顏色是可見的。但是,在VS2015中,將HTML文件加載到WebBrowser1控件後,交替行顏色不可見。VS 2015 - WebBrowser控件不會在CSS中顯示交替的表格行顏色

我已經檢查過其他地方和其他地方的其他帖子,很明顯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 /網頁瀏覽器 - 不適用 - CSS-正確?論壇= WPF –

回答

0

解決方案:幫助別人,我轉換爲Configuring the emulation mode of an Internet Explorer WebBrowser control, by Richard James Moss發佈到VB.NET C#的解決方案。

首先添加以下進口一個獨立的模塊(未例如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仿真將作出,所以噸在Web瀏覽器控件中可以看到完整的CSS功能。