如何使用WebKit.NET访问DOM?

3
我正在使用C#应用程序中的WebKit来渲染CSS样式的XML文档,并且我想添加DOM元素。如何才能访问DOM以执行此操作?问题似乎在于WebKitBrowser类中没有属性可以访问私有的webView成员。这可行吗?我需要修改类以添加它吗?还是我的做法错了?
1个回答

0

截至0.3.0版本,WebKit.Net似乎无法访问编译器对象模型(codedom)。 我做了一些稍微不同的事情,我使用了WebKit.Net,但我在应用程序中添加了一个HTTP监听器,并设置控件从那里检索页面。通过这种方式,我可以在同一应用程序中访问帖子和回调。其余的工作我通过页面上的JavaScript完成。

        HttpListener listener;

    public Form1( ) {
        InitializeComponent( );

        listener = new System.Net.HttpListener( );

        listener.Prefixes.Add( "http://*:88/" );
        listener.Start( );
        IAsyncResult result = listener.BeginGetContext( new AsyncCallback(  ListenerCallback ), listener );

    }

    public static void ListenerCallback( IAsyncResult result ) {

    string resp = "<body>test</body";

        HttpListener listener = ( HttpListener )result.AsyncState;

        // Call EndGetContext to complete the asynchronous operation.
        try {
            HttpListenerContext context = listener.EndGetContext( result );
            HttpListenerRequest request = context.Request;

            // Obtain a response object.
            HttpListenerResponse response = context.Response;

            // Construct a response.
            string webpage = request.Url.AbsolutePath.Substring( 1 );
            string responseString = null;
            if ( string.IsNullOrEmpty( webpage ) ) {
                responseString = resp;
            }
            else {
                webpage = webpage.Replace( ".", "_" );
                responseString = webkit_test.Properties.Resources.ResourceManager.GetResourceSet( System.Globalization.CultureInfo.CurrentCulture, true, false ).GetObject( webpage ) as string;
                if ( responseString == null ) {
                    responseString = "<html></html><body> 404 Web Page not Found</body>";
                }
            }
            byte[ ] buffer = System.Text.Encoding.UTF8.GetBytes( responseString );

            // Get a response stream and write the response to it.
            response.ContentType = "text/html; charset=UTF-8";
            response.ContentLength64 = buffer.Length;

            System.IO.Stream output = response.OutputStream;
            output.Write( buffer, 0, buffer.Length );
            output.Flush( );

            // You must close the output stream.
            output.Close( );
            IAsyncResult result1 = listener.BeginGetContext( new AsyncCallback( ListenerCallback ), listener );
        }
        catch { }
    }

顺便说一下,那段代码还可以从嵌入的资源中读取网页


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接