我在C#应用程序中使用WebKit来呈现CSS样式的XML文档,我希望能够添加DOM元素。如何访问DOM才能执行此操作?问题似乎是WebKitBrowser类中没有属性可以访问私有webView成员。可能吗?我是否需要修改类才能添加此项?我只是做错吗?
答案 0 :(得分:0)
从0.3.0开始,WebKit.Net似乎无法访问encodeom。 我做了一些稍微不同的事情,我使用了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 { }
}
顺便说一下。该代码还将从嵌入资源中读取网页