1

I'm trying to set my WebKitBrowser DocumentText to an HTML string containing a local SVG file path as image source. Actually I want to show the SVG file in a web browser. Here is my code:

        string SVGPath = "file:///D:/MySVGFiles 1/SVGSample01.svg";

        StringWriter stringWriter = new StringWriter();

        using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Src, SVGPath);
            writer.AddAttribute(HtmlTextWriterAttribute.Width, "50%");
            writer.AddAttribute(HtmlTextWriterAttribute.Height, "50%");

            writer.RenderBeginTag(HtmlTextWriterTag.Img); 
            writer.RenderEndTag(); 

        }

        string content = stringWriter.ToString();

        this.webKitBrowser1.DocumentText = content;

When I run the code, the browser only shows the image canvas, and does not render the SVG file. I have tried this with a JPG image too, and got the same result.

Could anyone please tell what is wrong with this code??

4

1 回答 1

1

我终于发现出了什么问题。WebKitBrowser 的 DocumentText 属性是一个字符串,在其 set 方法中,将 HTML 文本传递给 loadHTMLString 方法。

webView.mainFrame().loadHTMLString(value, null);

未指定 URL 时使用 DocumentText 属性。但是在这里我想从指定的地址加载图像。因此,如果使用诸如设置 DocumentText 属性之类的标签将无效。我不得不调用 loadHTMLString,当一个图像要使用它的地址添加到 HTML 字符串中时,URL 必须是图像文件的目录。根据我在https://groups.google.com/forum/#!topic/uni_webview/idiRRNIRnCU中找到的内容,我更改了代码,问题就解决了!这是有效的代码:

 string fileName = "SVGSample01.svg";
 string URL = "file:///D:/MySVGFiles 1/";

 StringWriter stringWriter = new StringWriter();

    using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
    {
        writer.AddAttribute(HtmlTextWriterAttribute.Src, fileName);
        writer.AddAttribute(HtmlTextWriterAttribute.Width, "50%");
        writer.AddAttribute(HtmlTextWriterAttribute.Height, "50%");

        writer.RenderBeginTag(HtmlTextWriterTag.Img); 
        writer.RenderEndTag(); 

    }

    string content = stringWriter.ToString();

 (this.webKitBrowser1.GetWebView() as IWebView).mainFrame().loadHTMLString(content,URL);

只需确保 URL 字符串包含“file:///”和最后一个“/”。

于 2015-03-01T10:13:24.747 回答