2011-07-28 44 views
1

我發佈了下面的完整代碼,所以你可以看到我在做什麼。webbrowser不刷新樣式表

情況: 我創建的IHTMLDocument2 currentDoc指向的DomDocument 我寫正確的琴絃 我關閉currentDoc

程序顯示我的HTML代碼,包括CSS的東西100%正確。工作

現在我想改變CSS,代替2列我將其設置爲3列 (只需改變寬度:48%至寬度:33%)

並用新的33重新運行代碼% 現在它突然不再應用任何CSS樣式。

當我關閉該程序,然後將CSS改變,再次爲33%,它的作品完美無瑕

所以,不知何故,而不設置完整的瀏覽器,我無法加載CSS一個第二次.. 或者,第一個CSS是在某些緩存中的某處,並與第二CSS衝突..只是在這裏謎......真的需要幫助如何解決這個問題

我搜索了互聯網和stackoverflow足夠長,我需要發佈此即使這個星球上的其他人已經將它發佈到某個地方,我也沒有找到它。

private void doWebBrowserPreview() 
    { 
     if (lMediaFiles.Count == 0) 
     { 
      return; 
     } 

     Int32 iIndex = 0; 
     for (iIndex = 0; iIndex < lMediaFiles.Count; iIndex++) 
     { 
      if (!lMediaFiles[iIndex].isCorrupt()) 
      { 
       break; 
      } 
     } 

     String strPreview = String.Empty; 
     String strLine = String.Empty; 

     // Set example Media 
     String strLinkHTM = lMediaFiles[iIndex].getFilePath(); 
     FileInfo movFile = new FileInfo(strLinkHTM + lMediaFiles[iIndex].getFileMOV()); 
     String str_sizeMB = (movFile.Length/1048576).ToString(); 
     if (str_sizeMB.Length > 3) 
     { 
      str_sizeMB.Insert(str_sizeMB.Length - 3, "."); 
     } 

     //Get info about our media files 
     MediaInfo MI = new MediaInfo(); 
     MI.Open(strLinkHTM + lMediaFiles[iIndex].getFileM4V()); 
     String str_m4vDuration = // MI.Get(0, 0, 80); 
     MI.Get(StreamKind.Video, 0, 74); 
     str_m4vDuration = "Duration: " + str_m4vDuration.Substring(0, 8) + " - Hours:Minutes:Seconds"; 
     String str_m4vHeightPixel = MI.Get(StreamKind.Video, 0, "Height"); // "Height (Pixel): " + 
     Int32 i_32m4vHeightPixel; 
     Int32.TryParse(str_m4vHeightPixel, out i_32m4vHeightPixel); 
     i_32m4vHeightPixel += 16; // for the quicktime embed menu 
     str_m4vHeightPixel = i_32m4vHeightPixel.ToString(); 
     String str_m4vWidthPixel = MI.Get(StreamKind.Video, 0, "Width"); //"Width (Pixel): " + 

     foreach (XElement xmlLine in s.getTemplates().getMovieHTM().Element("files").Elements("file")) 
     { 
      var query = xmlLine.Attributes("type"); 
      foreach (XAttribute result in query) 
      { 
       if (result.Value == "htm_header") 
       { 
        foreach (XElement xmlLineDes in xmlLine.Descendants()) 
        { 
         if (xmlLineDes.Name == "dataline") 
         { 
          strLine = xmlLineDes.Value; 
          strLine = strLine.Replace(@"%date%", lMediaFiles[iIndex].getDay().ToString() + " " + lMediaFiles[iIndex].getMonth(lMediaFiles[iIndex].getMonth()) + " " + lMediaFiles[iIndex].getYear().ToString()); 
          strPreview += strLine + "\n"; 
         } 
        } 
       } 
      } 
     } 

     strLine = "<style type=\"text/css\">" + "\n"; 
     foreach (XElement xmlLine in s.getTemplates().getLayoutCSS().Element("layoutCSS").Elements("layout")) 
     { 
      var query = xmlLine.Attributes("type"); 
      foreach (XAttribute result in query) 
      { 
       if (result.Value == "layoutMedia") 
       { 
        foreach (XElement xmlLineDes in xmlLine.Elements("layout")) 
        { 
         var queryL = xmlLineDes.Attributes("type"); 
         foreach (XAttribute resultL in queryL) 
         { 
          if (resultL.Value == "layoutVideoBox") 
          { 
           foreach (XElement xmlLineDesL in xmlLineDes.Descendants()) 
           { 
            if (xmlLineDesL.Name == "dataline") 
            { 
             strLine += xmlLineDesL.Value + "\n"; 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
     strLine += "</style>" + "\n"; 
     strPreview = strPreview.Insert(strPreview.LastIndexOf("</head>", StringComparison.Ordinal), strLine); 


     for (Int16 i16Loop = 0; i16Loop < 3; i16Loop++) 
     { 
      foreach (XElement xmlLine in s.getTemplates().getMovieHTM().Element("files").Elements("file")) 
      { 
       var query = xmlLine.Attributes("type"); 
       foreach (XAttribute result in query) 
       { 
        if (result.Value == "htm_videolist") 
        { 
         foreach (XElement xmlLineDes in xmlLine.Descendants()) 
         { 
          if (xmlLineDes.Name == "dataline") 
          { 
           strLine = xmlLineDes.Value; 
           strLine = strLine.Replace(@"%m4vfile%", strLinkHTM + lMediaFiles[iIndex].getFileM4V()); 
           strLine = strLine.Replace(@"%moviefile%", strLinkHTM + lMediaFiles[iIndex].getFileMOV()); 
           strLine = strLine.Replace(@"%height%", str_m4vHeightPixel); 
           strLine = strLine.Replace(@"%width%", str_m4vWidthPixel); 
           strLine = strLine.Replace(@"%duration%", str_m4vDuration); 
           strLine = strLine.Replace(@"%sizeMB%", str_sizeMB); 
           strLine = strLine.Replace(@"%date%", lMediaFiles[iIndex].getDay().ToString() + " " + lMediaFiles[iIndex].getMonth(lMediaFiles[iIndex].getMonth()) + " " + lMediaFiles[iIndex].getYear().ToString()); 
           strPreview += strLine + "\n"; 
          } 
         } 
        } 
       } 
      } 
     } 

     foreach (XElement xmlLine in s.getTemplates().getMovieHTM().Element("files").Elements("file")) 
     { 
      var query = xmlLine.Attributes("type"); 
      foreach (XAttribute result in query) 
      { 
       if (result.Value == "htm_footer") 
       { 
        foreach (XElement xmlLineDes in xmlLine.Descendants()) 
        { 
         if (xmlLineDes.Name == "dataline") 
         { 
          strPreview += xmlLineDes.Value + "\n"; 
         } 
        } 
       } 
      } 
     } 
     webBrowserPreview.Navigate("about:blank"); 
     webBrowserPreview.Document.OpenNew(false); 
     mshtml.IHTMLDocument2 currentDoc = (mshtml.IHTMLDocument2)webBrowserPreview.Document.DomDocument; 
     currentDoc.clear(); 
     currentDoc.write(strPreview); 
     currentDoc.close(); 

     /* 
     try 
     { 
      if (webBrowserPreview.Document != null) 
      { 
       IHTMLDocument2 currentDocument = (IHTMLDocument2)webBrowserPreview.Document.DomDocument; 
       int length = currentDocument.styleSheets.length; 

       IHTMLStyleSheet styleSheet = currentDocument.createStyleSheet(@"", 0); 
       //length = currentDocument.styleSheets.length; 
       //styleSheet.addRule("body", "background-color:blue"); 
       strLine = String.Empty; 
       foreach (XElement xmlLine in s.getTemplates().getLayoutCSS().Element("layoutCSS").Elements("layout")) 
       { 
        var query = xmlLine.Attributes("type"); 
        foreach (XAttribute result in query) 
        { 
         if (result.Value == "layoutMedia") 
         { 
          foreach (XElement xmlLineDes in xmlLine.Elements("layout")) 
          { 
           var queryL = xmlLineDes.Attributes("type"); 
           foreach (XAttribute resultL in queryL) 
           { 
            if (resultL.Value == "layoutVideoBox") 
            { 
             foreach (XElement xmlLineDesL in xmlLineDes.Descendants()) 
             { 
              if (xmlLineDesL.Name == "dataline") 
              { 
               strLine += xmlLineDesL.Value; 
              } 
             } 
            } 
           } 
          } 
         } 
        } 
       } 
       //TextReader reader = new StreamReader(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "basic.css")); 
       //string style = reader.ReadToEnd(); 
       styleSheet.cssText = strLine; 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     }*/ 

     webBrowserPreview.Refresh(); 
    } 
+0

所以它只是緩存你的CSS?你有沒有做過硬刷新? (CTRL + F5) – abney317

+0

是的。我也嘗試了許多不同的變體。包括將文件存儲到磁盤並使用導航。事情是:出於某種原因,一個名爲「div#video_box」的樣式表在會話期間無法更改。即使我把webbrowser放在一個自己的窗體中,並打開/關閉這個窗體,c#應該自動處理,我不能改變樣式表。如果我在CSS和HTML中重命名它,它會正確加載更改後的數據(一次!),但每次我改變某些內容時,都必須重新命名。我可以用枚舉器想出一些旁路解決方案,但這當然不是一個好的解決方案。 – Vince

+0

http://www.eggheadcafe.com/software/aspnet/30119924/dynamic-stylesheet-problem.aspx我認爲這是一個類似的問題描述。在c#webbrowser實現中看起來像一些奇怪的事情 – Vince

回答

0

我現在已經成功地實施了錇銳方法到我的項目 具有相同的錯誤! 找到解決方案!

第一次嘗試沒有工作: 我有一個持久的窗體(主窗體),裏面嵌套了一個WebBrowser。 改變它的CSS後,我告訴它導航到這個新的HTML!

這也沒有工作: 然後我試圖把webbrowser自己的形式。我只需打開/關閉每個 我需要刷新一次。確保垃圾收集器清理所有內容

然後我嘗試了Berkelium並將其重寫爲我的需要: 與使用webbrowser的嘗試2的邏輯相同。沒有運氣。

所以我試圖打開Firefox本身,看看我是否可以用真正的瀏覽器來模擬這種行爲。確實!當我打開Firefox,並強制打開文件(如果你只是打開一個新文件,Firefox實際上並沒有導航到它,但檢測到這已被打開,只是刷新它) 我注意到這是由於快速打開頁! 在1個Firefox會話中強制打開同一文件兩次(導航)的小腳本具有相同的效果:所有CSS損壞!

因此,出於某種原因,您不應該導航同一個文件兩次,而不是關閉任何東西,只需強制刷新!沒有一個「導航」

希望這些信息可以幫助別人,因爲我失去了很多時間找出它是「導航」,以相同的文件,曾多次造成樣式表的腐敗