WPF WebBrowser and special characters like german “umlaute”

戏子无情 提交于 2019-11-29 08:49:29
Gavin Jones

This is very quirky.

  1. My solution was to put an explicit meta tag in my HTML file - "My Page.html"

    <meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>
    
  2. Then using the standard Web Browser .NET control I then created a URI object first.

    webBrowser1.Url = new Uri("My Page.html");
    
  3. Then draw the page using the refresh method.

    webBrowser1.Refresh();
    

Note if you use the Navigate method directly it fails to pick up the utf-8 directive, but the URI and refresh approach does.

Quirky, but it works.

BennoDual

I have solved it with the following:

    static void webBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e) {
        var webBrowser = sender as WebBrowser;
        if(webBrowser == null) {
            return;
        }
        var doc = (IHTMLDocument2)webBrowser.Document;           

        doc.charset = "utf-8";
        webBrowser.Refresh();
    }

The WebBrowser control uses Internet Explorer internally, whichever version you have on your local PC. If you can fix the problem in IE, it should be fixed in the WebBrowser control.

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