SharedBufferReceivedEvent class
事件的事件 chrome.webview.sharedbufferreceived
物件。 成功呼叫 時 CoreWebView2.PostSharedBufferToScript
,會分派此事件。
- Extends
備註
範例
下列範例會將數據傳送至腳本,以供一次性只讀取用。
首先,在原生主應用程式程式代碼中,將資料設定為共用記憶體:
wil::com_ptr<ICoreWebView2ExperimentalEnvironment10> environment;
CHECK_FAILURE(
m_appWindow->GetWebViewEnvironment()->QueryInterface(IID_PPV_ARGS(&environment)));
wil::com_ptr<ICoreWebView2ExperimentalSharedBuffer> sharedBuffer;
CHECK_FAILURE(environment->CreateSharedBuffer(bufferSize, &sharedBuffer));
// Add data to shared memory via IStream.
wil::com_ptr<IStream> stream;
CHECK_FAILURE(sharedBuffer->OpenStream(&stream));
CHECK_FAILURE(stream->Write(data, sizeof(data), nullptr));
PCWSTR additionalDataAsJson = L"{\"myBufferType\":\"bufferType1\"}";
if (fromFrame)
{
m_webviewFrame4->PostSharedBufferToScript(
sharedBuffer.get(), COREWEBVIEW2_SHARED_BUFFER_ACCESS_READ_ONLY,
additionalDataAsJson);
}
else
{
m_webView18->PostSharedBufferToScript(
sharedBuffer.get(), COREWEBVIEW2_SHARED_BUFFER_ACCESS_READ_ONLY,
additionalDataAsJson);
}
// Close the one-time shared buffer, to release resources.
sharedBuffer->Close();
在 HTML 檔中,訂閱並處理 sharedbufferreceived 事件。
接下來,在 HTML 檔中訂閱 ,然後處理 事件 sharedbufferreceived
:
window.chrome.webview.addEventListener("sharedbufferreceived", e => {
SharedBufferReceived(e);});
let readOnlySharedBuffer;
function ShowReadOnlySharedBuffer() {
if (readOnlySharedBuffer) {
DisplaySharedBufferData(readOnlySharedBuffer);
} else {
// Post a web message to ask host to share the one time read only buffer.
chrome.webview.postMessage("RequestOneTimeShareBuffer");
}
}
function DisplaySharedBufferData(buffer) {
document.getElementById("shared-buffer-data").value =
new TextDecoder().decode(new Uint8Array(buffer));
}
function SharedBufferReceived(e) {
if (e.additionalData && e.additionalData.myBufferType == "bufferType1") {
readOnlySharedBuffer = e.getBuffer();
} else {
sharedBuffer = e.getBuffer();
}
DisplaySharedBufferData(e.getBuffer());
}
function ReleaseBuffer(buffer) {
window.chrome.webview.releaseBuffer(buffer);
}
屬性
additional |
物件,這是將 參數 |
source | 事件的來源是 |
方法
get |
|
屬性詳細資料
additionalData
物件,這是將 參數CoreWebView2.PostSharedBufferToScript
剖析additionalDataAsJson
為 JSON 字串的結果。 如果 additionalDataAsJson
是 nullptr
或空字串,則這個屬性會是 undefined
。
additionalData: any;
屬性值
any
source
方法詳細資料
getBuffer()
ArrayBuffer
傳回物件,其中包含傳遞至 CoreWebView2.PostSharedBufferToScript
之共用緩衝區的支持內容。 如果 CoreWebView2.PostSharedBufferToScript
呼叫 時將緩衝區設定為 ReadOnly
,則只允許讀取存取緩衝區。 如果您嘗試修改只讀緩衝區中的內容,將會在 WebView 轉譯器進程中造成存取違規,並使轉譯器進程損毀。
getBuffer(): ArrayBuffer;
傳回
ArrayBuffer
傳遞 ArrayBuffer
至 CoreWebView2.PostSharedBufferToScript
之共享緩衝區的 。