2016-07-28 132 views
0

我有一個HTML頁面,在C#項目中生成。它的工作原理,當我在IE中打開此頁面。Windows.Forms.WebBrowser加載頁面本地SVG文件

<!DOCTYPE HTML> 
 
<meta http-equiv='X-UA-Compatible' content='IE=10' /> 
 
<html lang='en'> 
 
<head> 
 
    <title>TemplateSVG</title> 
 
    <script type='text/javascript' src='InterfaceSVG.js'></script> 
 
</head> 
 
<body style='margin: 0; overflow: hidden;'> 
 
    <div class="page-content"> 
 
     <object id='idSVG' type='image/svg+xml' data='D:\Examples\ExampleFla.svg'></object> 
 
    </div> 
 
</body> 
 
</html>

我裝獲取文本在Web瀏覽器中

if (webBrowser.Document == null) 
    { 
     webBrowser.DocumentText = theHTMLtext; 
    } 
    else 
    { 
     webBrowser.Document.OpenNew(true); 
     webBrowser.DocumentText = theHTMLtext; 
    } 

但文件InterfaceSVG.js沒有找到。

當我給完整路徑js文件src='D:\[Path]\InterfaceSVG.js' JS腳本生成與getSVGDocument()一致的異常。

var SvgDoc; 
 
window.addEventListener('load', function() { 
 
    SvgDoc = document.getElementById("idSVG"); 
 
    if (SvgDoc == null) { alert("error"); return; } 
 
    SvgDoc = SvgDoc.getSVGDocument(); // IE created Access Deny. 
 
});

編輯: 嘗試插入來自js文件的文本。

<script>Text from InterfaceSVG.js </scipt>  

但它產生相同的異常(訪問拒絕)對符合getSVGDocument()

我的文件夾中使用SVG文件和使用功能Navigate代替DocumentText保存的結果HTML頁面。現在它可以工作......但我不想在磁盤上寫任何東西。

string path = Path.GetDirectoryName(pathToSvgFile); 
    string file = "\\"+Path.GetFileNameWithoutExtension(pathToSvgFile); 
    string newfile = path + file; 
    File.WriteAllText(newfile, theHTMLtext); 
    webBrowser.Navigate(newfile); 

回答

0

我發現需要打開一個用戶頁面。

  1. 創建模板HTML頁面沒有腳本。

<!DOCTYPE HTML> 
 
<meta http-equiv='X-UA-Compatible' content='IE=10' /> 
 
<html lang='en'> 
 
<head> 
 
    <title>Empty Page</title> 
 
</head> 
 
<body style='margin: 0; overflow: hidden; '> 
 
    <div class="page-content"> 
 
     <object id='idSVG' style='height: 100%; width: 100%; position:absolute;' type='image/svg+xml' data='{0}'></object> 
 
    </div> 
 
</body> 
 
</html>

  • 添加腳本文件,並更改HTML頁面的正文你在事件需要webBrowser.Navigated。

    static string PathToSvgFile; 
        public static void OpenSVG(this WebBrowser webBrowser, string pathToSvgFile) 
        { 
         if (webBrowser.ReadyState == WebBrowserReadyState.Complete || webBrowser.ReadyState == WebBrowserReadyState.Uninitialized) 
         { 
          webBrowser.Navigate([Path to emptyPage.html]); 
          PathToSvgFile = pathToSvgFile; 
          webBrowser.Navigated += webBrowser_Navigated; 
         } 
        } 
    
        private static void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e) 
        { 
         var webBrowser = ((WebBrowser)sender); 
         HtmlElementCollection head = webBrowser.Document.GetElementsByTagName("head"); 
    
         if (head != null) 
         { 
          var element = webBrowser.Document.CreateElement("script"); 
          element.SetAttribute("type", "text/javascript"); 
          var elementDom = (MSHTML.IHTMLScriptElement)element.DomElement; 
          elementDom.src = [JavaScriptFile.js]; 
          ((HtmlElement)head[0]).AppendChild(element); 
         } 
    
         webBrowser.Document.Body.InnerHtml = String.Format(webBrowser.Document.Body.InnerHtml, PathToSvgFile); 
         webBrowser.Navigated -= webBrowser_Navigated; 
        }