Windows Phone 7 WebBrowser 中文乱码问题

时间:2022-04-25
本文章向大家介绍Windows Phone 7 WebBrowser 中文乱码问题,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

通过WebBrowser直接请求网页,是正常显示的,只是通过获取到字符串,再通过NavigateToString()就会显示乱码.

中文转换成 Unicode编码就可以了 :

    public static string Unicode2HTML(string HTML)
    {
            StringBuilder str = new StringBuilder();
            char c;
            for (int i = 0; i < HTML.Length; i++)
            {
                c = HTML[i];
                if (Convert.ToInt32(c) > 127)
                {
                    str.Append("&#" + Convert.ToInt32(c) + ";");
                }
                else
                {
                    str.Append(c);
                }
            } 
            return str.ToString(); 
       }
       private void RenderPage()
        {
            var html = FxConstants.ArticleViewTemplate
                .Replace("{article-header}", _SelectedRssItem.Title)
                .Replace("{article-content}", _Content)
                .Replace("{background-specific-style}",
                         PhoneUI.CurrentPhoneBackground == PhoneBackground.Dark
                             ? Fx.Instance.Settings["Article-View-DarkBackground-CSS"]
                             : Fx.Instance.Settings["Article-View-LightBackground-CSS"])
                .Replace("{common-style}", Fx.Instance.Settings["Article-View-CSS"]);
            html = Unicode2HTML(html);
            Browser.NavigateToString(html);
        }