Fork me on GitHub

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

        }

posted @ 2011-12-11 21:33  张善友  阅读(1554)  评论(0编辑  收藏  举报