您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> .NET >> WP7,让WebBrowser从独立存储中加载css/js文件以及图片文件

WP7,让WebBrowser从独立存储中加载css/js文件以及图片文件(1/2)

来源:网络整理     时间:2016/5/18 7:08:32     关键词:webbrowser

关于网友提出的“WP7,让WebBrowser从独立存储中加载css/js文件以及图片文件”问题疑问,本网通过在网上对“WP7,让WebBrowser从独立存储中加载css/js文件以及图片文件”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题:WP7,让WebBrowser从独立存储中加载css/js文件以及图片文件
描述:

介绍:

在我们的Windows Phone 7项目中,有可能需要动态生成一些HTML字符串或者从WebService上获取一段HTML字符串,然后将这些HTML字符串在WebBrowser控件上显示,那么一般在这种情况下你可能需要将对应的js文件,css文件以及图片文件保存在应用程序安装包内而非从网络上获取。

解决方案:

为了演示如何做到这一点,我们演示一个来自从webservice上获取的html代码。

Do you agree to this request?

如上图,html需要调用来自jquery Mobile的data-role,在我们这个演示中,将在xap安装包中将包含jqueryMobile的所有js文件以及css文件。

cppwebbrowser,webbrowser下载,webbrowserprocess,joker game,webbrowser控件

首先要添加这些文件到项目的指定目录中,然后将这些文件的"Build Action"设置为"Content"。接着下一步我们先创建一个构建HTML的方法BuildHTML将拼接来自WebService的html代码。

public string BuildHTML(string htmlFromWS)
{
    StringBuilder html = new StringBuilder(@"
                    
                    

                        
                        

                    
                    
                        
"); html.Append(htmlFromWS); html.Append("
"); return html.ToString(); }

如上图,所有的css文件以及js文件的加载路径都是/html/... 这种形式,这就意味着webbrowser控件将通过相对Uri从独立存储中加载这些文件。但不幸的是我们无法将这些文件就这么直接添加到独立存储中,而只能先添加到解决方案目录里面,所以这里需要创建一个方法来完成这个需求 CopyContentToIsolatedStorage.

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

相关图片

相关文章