如何在webkit-sharp webview对象中修改UserAgent?

时间:2013-06-28 13:36:45

标签: c# mono webkit-sharp

WebKit.WebView有一个Settings.UserAgent选项,但只有一个get()方法defind,所以我无法设置该值。我似乎无法在其他任何地方找到它。

1 个答案:

答案 0 :(得分:0)

好的,这是怎么做的......

重写WebKit.WebSettings类并访问受GLib.Object保护的成员SetProperty:

class ExposedWebSettings : WebKit.WebSettings {
    public void g_object_set(string name, GLib.Value value) {
        SetProperty(name, value);
    }
}

然后你可以实例化这个对象并调用g_object_set来设置任何属性......

WebView webView = new WebView();
ExposedWebSettings settings = new ExposedWebSettings();
settings.g_object_set("user-agent", new GLib.Value("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120427 Firefox/15.0a1"));
webView.Settings = settings;
webView.Open ("http://www.google.com");

顺便说一下,这就是GLib.Object.SetProperty的样子:

protected void SetProperty (string name, Value val)
{
    IntPtr intPtr = Marshaller.StringToPtrGStrdup (name);
    Object.g_object_set_property (this.Raw, intPtr, ref val);
    Marshaller.Free (intPtr);
}