共用方式為


CoreWebView2.PostWebMessageAsJson Method

Definition

Overloads

PostWebMessageAsJson(String)

Posts the specified webMessageAsJson to the top level document in this WebView.

PostWebMessageAsJson(String, List<Object>)

Same as PostWebMessageAsJson(String), but also has support for posting DOM objects to page content.

PostWebMessageAsJson(String)

Posts the specified webMessageAsJson to the top level document in this WebView.

public void PostWebMessageAsJson (string webMessageAsJson);
member this.PostWebMessageAsJson : string -> unit
Public Sub PostWebMessageAsJson (webMessageAsJson As String)

Parameters

webMessageAsJson
String

The web message to be posted to the top level document in this WebView.

Examples

Runs the message event of the window.chrome.webview of the top-level document. JavaScript in that document may subscribe and unsubscribe to the event using the following code:

window.chrome.webview.addEventListener('message', handler)
window.chrome.webview.removeEventListener('message', handler)

Remarks

The event args is an instance of MessageEvent. The IsWebMessageEnabled setting must be true or the message will not be sent. The event arg's data property of the event arg is the webMessageAsJson string parameter parsed as a JSON string into a JavaScript object. The event arg's source property of the event arg is a reference to the window.chrome.webview object. For information about sending messages from the HTML document in the WebView to the host, navigate to WebMessageReceived. The message is sent asynchronously. If a navigation occurs before the message is posted to the page, the message is not be sent.

See also

Applies to

PostWebMessageAsJson(String, List<Object>)

Same as PostWebMessageAsJson(String), but also has support for posting DOM objects to page content.

public void PostWebMessageAsJson (string webMessageAsJson, System.Collections.Generic.List<object> additionalObjects);
member this.PostWebMessageAsJson : string * System.Collections.Generic.List<obj> -> unit
Public Sub PostWebMessageAsJson (webMessageAsJson As String, additionalObjects As List(Of Object))

Parameters

webMessageAsJson
String

The web message to be posted to the top level document in this WebView.

additionalObjects
List<Object>

Additional DOM objects posted to the content.

Remarks

The event args is an instance of MessageEvent. The IsWebMessageEnabled setting must be true or the message will not be sent. The event arg's data property of the event arg is the webMessageAsJson string parameter parsed as a JSON string into a JavaScript object. The event arg's source property of the event arg is a reference to the window.chrome.webview object. For information about sending messages from the HTML document in the WebView to the host, navigate to WebMessageReceived. The message is sent asynchronously. If a navigation occurs before the message is posted to the page, the message is not be sent. This additionalObjects is retrieved in web content via the DOM MessageEvent additionalObjects property as an array-like list of DOM objects. Currently these type of objects can be posted:

.NET / WinRT DOM type
CoreWebView2FileSystemHandle [FileSystemHandle](https://developer.mozilla.org/docs/Web/API/FileSystemHandle)
null null
The objects are posted to web content, following the

structured-clone semantics, meaning only objects that can be cloned can be posted. They will also behave as if they had been created by the web content they are posted to. For example, if a FileSystemFileHandle is posted to a web content it can only be re-transferred via postMessage to other web content with the same origin. Warning: An app needs to be mindful when using this API to post DOM objects as this API provides the web content with unusual access to sensitive Web Platform features such as filesystem access! Similar to PostWebMessageAsJson, the app should check the Source property right before posting the message to ensure the message and objects will only be sent to the target web content that it expects to receive the DOM objects. Additionally, the order of messages that are posted between PostWebMessageAsJson and PostWebMessageAsJsonWithAdditionalObjects may not be preserved.

See also

Applies to