1

我在下面发布了完整的代码,所以你可以看到我在做什么。

情况:我创建了一个指向 DomDocument 的 IHTMLDocument2 currentDoc 我写了正确的字符串我关闭了 currentDoc

程序向我展示了 html 代码,包括 100% 正确的 CSS 内容。作品

现在我想更改 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();
    }
4

1 回答 1

0

我现在成功地在我的项目中实现了 berkelium-sharp 方法有同样的错误!找到了解决方案!

第一次尝试失败:我有一个持久表单(主表单),其中有一个嵌套的 WebBrowser。用它的 css 更改 html 后,我告诉它导航到这个新的 html!

这也不起作用:然后我尝试将 webbrowser 放在自己的表单上。每次我需要刷新时,我都会打开/关闭它。确保垃圾收集器清理一切

然后我尝试了 Berkelium 并根据我的需要重写了它:与使用网络浏览器的尝试 2 相同的逻辑。也没有运气。

所以我尝试打开firefox本身,看看我是否可以用真正的浏览器模拟这种行为。的确!当我打开 firefox 并强制打开文件时(如果您只是打开一个新文件,firefox 实际上并没有导航到它,而是检测到它已经打开并简单地刷新它)我注意到这是由于页面的快速打开!在 1 个 firefox 会话中强制打开同一个文件两次(导航)的小脚本具有相同的效果:所有 CSS 都损坏了!

所以,出于某种原因,你不应该两次浏览同一个文件,而是不要关闭任何东西,只需强制刷新!不是“导航”

希望这个信息可以帮助其他人,因为我花了很多时间发现它是“导航”到同一个文件,而不是一次导致样式表损坏

于 2011-07-29T22:47:05.347 回答