开发者

webbrowser not refreshing stylesheet

I post the complete code below, so you can see what I'm doing.

Situation: I create a IHTMLDocument2 currentDoc pointing to the DomDocument I write the proper string I close the currentDoc

program shows me the html code including the CSS stuff 100% correct. Works

Now I want to change the CSS, instead of 2 columns I set it to 3 columns (Simply change the width:48% to width:33%)

and rerun the code with开发者_StackOverflow the new 33% now it suddenly doesn't apply any CSS style anymore.

When I close the program, and then change the CSS to 33% again, it works flawless

So, somehow, without disposing the complete webbrowser, I can't load the CSS a 2nd time.. or, the first CSS is somewhere in some cache, and conflicts with the 2nd CSS.. Just riddling here.. really need help on how to solve this

I searched the internet and stackoverflow long enough that I need to post this, even if someone else on this planet already posted it somewhere, I didn't find it.

    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();
    }


I now successfully implemented the berkelium-sharp method to my project Has the same bug! Found a solution!

First attempt which didn't work: I had a persistent form (main form) and inside it a nested WebBrowser. After changing the html with it's css, i told it to navigate to this new html!

This didn't work either: Then I tried putting webbrowser on an own form. Which I simply open/close each time I need a refresh. TO be sure the garbage collector cleans everything

Then I tried the Berkelium and rewrote it to my needs: same logic as attempt 2 with the webbrowser. No luck either.

So I tried to open firefox itself and see if I can emulate this behaviour with a real browser. Indeed! When I open firefox, and force open the file (if you simply open a new file, firefox doesn't actually navigate to it, but detects this was already opened and simply refreshes it) I noticed this due to the fast opening of the page! A little scripting to force opening the same file twice (navigating) in 1 firefox session had the same effect: all CSS corrupt!

so, for some reason, you shouldn't navigate the same file twice, but instead of closing anything, simply force a refresh! Not a "Navigate"

Hope this info can help others, since I lost a lot of time finding out that it is the "navigate" to the same file more then once causing the corruption of stylesheets

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜