WP8 - WebBrowser控件,用于显示资源文件夹中的图像

时间:2014-04-29 06:50:40

标签: c# html image windows-phone-8 webbrowser-control

我正在处理WP8应用程序,我在位置Resources\Graphics\中的图像很少我试图显示这些文件夹中的图像,但它没有选择路径。

这是我的代码:

<img src=\"/Resources;component/Graphics/"+ImageName).Append("\" ")这是我在我的WebBrowserControl中使用的字符串。

WebBrowserControl.NavigateToString(html); // here html is a string which has all the html code in it.

但它不显示图像。这里有什么问题解决这个问题?

2 个答案:

答案 0 :(得分:1)

当我尝试打开已保存到本地存储的图像时,我遇到了类似的问题。我可以通过将HTML文件放在同一位置来解决这个问题,以便我可以使用"./filename.ext"访问这些文件。 它不适用于我在webview中访问本地文件时使用的任何资源路径构造。

答案 1 :(得分:0)

实现此目的的唯一方法是将图像和动态生成的Html内容文件存储在应用程序的独立存储中。

通过此链接了解如何将内容存储在独立存储中并使用Web浏览器控件显示它:http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff431811(v=vs.105).aspx

你走了, 首先,使用以下方法将要在Html文件上显示的图像存储到应用程序的独立存储中。

public static void CopyContentToIsolatedStorage(string file)
        {
            // Obtain the virtual store for the application.
            IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();

            if (iso.FileExists(file))
                return;

            var fullDirectory = System.IO.Path.GetDirectoryName(file);
            if (!iso.DirectoryExists(fullDirectory))
                iso.CreateDirectory(fullDirectory);

            // Create a stream for the file in the installation folder.
            using (Stream input = Application.GetResourceStream(new Uri(file, UriKind.Relative)).Stream)
            {
                // Create a stream for the new file in isolated storage.
                using (IsolatedStorageFileStream output = iso.CreateFile(file))
                {
                    // Initialize the buffer.
                    byte[] readBuffer = new byte[4096];
                    int bytesRead = -1;

                    // Copy the file from the installation folder to isolated storage. 
                    while ((bytesRead = input.Read(readBuffer, 0, readBuffer.Length)) > 0)
                    {
                        output.Write(readBuffer, 0, bytesRead);
                    }
                }
            }

通过调用方法,

CopyContentToIsolatedStorage("Graphics/ImageName.png");//Pass the image folder path here

然后,动态形成Html,如下所示

StringBuilder HtmlString = new StringBuilder(@"<html><head><title>Test html File</title></head>");
HtmlString.Append(@"<body>");
HtmlString.Append(@"<img src=""Graphics/""@"+ ImageName + "/>");
HtmlString.Append(@"</body></html>");

然后使用以下代码将Html文件保存到隔离存储中

var bytes = Encoding.UTF8.GetBytes(html.ToString());
using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (IsolatedStorageFileStream output = iso.CreateFile("Testfile.html"))
    {
        output.Write(bytes, 0, bytes.Length);
    }
}

现在使用您的浏览器控件调用该html文件,如下所示

WebBrowserControl.Navigate(new Uri("Testfile.html", UriKind.Relative));
相关问题