Browser control NavigateToString display HTML code instead of rendering page

…衆ロ難τιáo~ 提交于 2019-12-10 10:05:15

问题


I am developing a browser app using Windows Phone 8 browser control.

The app download an external webpage using WebClient into a string in the background. Then the browser navigate to the content using

webBrowser.NavigateToString(str);

However, instead of rendering the page, the browser shows the HTML code. I thought since no changes were made to the string, NavigateToString should handle it seamlessly. Or perhaps I am missing something.

So how do I display the HTML page instead of its code?

EDIT

Here's some of my code

        webClient = new WebClient();
        webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
        webClient.DownloadStringAsync(new Uri(uri));



    private  void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
         PageString = e.Result;
    }

    ...

     webBrowser.NavigateToString(PageString); 

回答1:


This is an issue with Windows Phone 8.

Here you have a workaround.




回答2:


When you use DownloadStringAsync, it also downloads the DOCTYPE declaration. You can remove this and start your code with the <html> block as NavigateToString doesn't seem to like the <!DOCTYPE HTML> declaration.

webClient = new WebClient();
webClient.DownloadStringCompleted += webClient_DownloadStringCompleted;
webClient.DownloadStringAsync(new Uri(uri));

void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    //remove "<!DOCTYPE HTML>"
    PageString = e.Result.Replace("<!DOCTYPE HTML>","").Trim();        
}

webBrowser.NavigateToString(PageString);



回答3:


Documentation for WebBrowser.NavigateToString says:

If the text parameter is not in valid HTML format, it will be displayed as plain text.

Can you check if str is in valid HTML format?

private  void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
     PageString = e.Result;
     webBrowser.NavigateToString(PageString); 
}



回答4:


Another Way:

        wb.Navigate("");
        do
        {
            Application.DoEvents();
        } while ((wb.ReadyState != WebBrowserReadyState.Complete));
        wb.Document.Body.InnerHtml = "Html";


来源:https://stackoverflow.com/questions/16488082/browser-control-navigatetostring-display-html-code-instead-of-rendering-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!