Bewerken

Delen via


WKWebView.EvaluateJavaScriptAsync Method

Definition

Overloads

EvaluateJavaScriptAsync(NSString)

Evaluates the given JavaScript string.

EvaluateJavaScriptAsync(String)

Evaluates the given JavaScript string.

EvaluateJavaScriptAsync(NSString)

Evaluates the given JavaScript string.

public virtual System.Threading.Tasks.Task<Foundation.NSObject> EvaluateJavaScriptAsync (Foundation.NSString javascript);
abstract member EvaluateJavaScriptAsync : Foundation.NSString -> System.Threading.Tasks.Task<Foundation.NSObject>
override this.EvaluateJavaScriptAsync : Foundation.NSString -> System.Threading.Tasks.Task<Foundation.NSObject>

Parameters

javascript
NSString

The JavaScript string to evaluate

Returns

A task that represents the asynchronous EvaluateJavaScript operation. The value of the TResult parameter is a WKJavascriptEvaluationResult.

Remarks

This method will throw a NSErrorException if the JavaScript is not evaluated successfully.

var config = new WKWebViewConfiguration();
var wk = new WKWebView(UIScreen.MainScreen.Bounds, config);
var js = (NSString) "document.getElementById('foo').innerHTML = 'bar'";
var result = await wk.EvaluateJavaScriptAsync(js); //== "bar"

The EvaluateJavaScriptAsync method is suitable to be used with C# async by returning control to the caller with a Task representing the operation.

The system calls completionHandler after evaluation. The arguments to the handler are an NSObject containing the results of the evaluation and an NSError if an error. If an error occurred, the result argument will be null. If no error occurred, the error argument will be null.

var config = new WKWebViewConfiguration();
var wk = new WKWebView(UIScreen.MainScreen.Bounds, config);
var js = (NSString) "document.getElementById('foo').innerHTML = 'bar'";
WKJavascriptEvaluationResult handler = (NSObject result, NSError err) => { 
  if(err != null)
  {
    System.Console.WriteLine(err);
  }
  if(result != null)
  {
     System.Console.WriteLine(result);
  }
};
wk.EvaluateJavaScript(js, handler);

Applies to

EvaluateJavaScriptAsync(String)

Evaluates the given JavaScript string.

public System.Threading.Tasks.Task<Foundation.NSObject> EvaluateJavaScriptAsync (string javascript);
member this.EvaluateJavaScriptAsync : string -> System.Threading.Tasks.Task<Foundation.NSObject>

Parameters

javascript
String

A well-formed JavaScript expression.

Returns

A task that represents the asynchronous EvaluateJavaScript operation. The TResult holds the results of the evaluation.

Remarks

This method will throw a NSErrorException if the JavaScript is not evaluated successfully.

var config = new WKWebViewConfiguration();
var wk = new WKWebView(UIScreen.MainScreen.Bounds, config);
var js = (NSString) "document.getElementById('foo').innerHTML = 'bar'";
var result = await wk.EvaluateJavaScriptAsync(js); //== "bar"

Applies to