I have created UWP app which has a webview embedded in it. The webview is loaded with URL of a web app. The web app has a JavaScript object added to the window object. example:window.html_communicator
The web app uses this html_communicator object and calls certain methods on it to invoke callbacks on the UWP application.
for example, there can be a method defined on html_communicator called onLocationSelect and the web app will call this method upon some user interaction. The UWP app registers to the methods by calling addWebAllowedObject on the WebView.
The callbacks are working fine when I disabled .Net Native Tool Chain. But these callbacks are not working fine when .Net Natvie Tool Chain is enabled.
Below is a javascript code snippet that calls the bridge method on button click.
var html_communicator = html_communicator || {};
window.onload = function () {
document.getElementById("locationSelect")
.addEventListener("click", () => {
if (html_communicator && html_communicator.onLocationSelect) {
document.getElementById("msg").innerHTML = `Successfully called bridge method: Button clicked at ${new Date()}`;
sumorea_app.onLocationSelect();
}
else {
document.getElementById("msg").innerHTML = `Failed to call bridge method: Button clicked at ${new Date()}`;
}
})
}
CodePudding user response:
The callbacks are working fine when I disabled .Net Native Tool Chain. But these callbacks are not working fine when .Net Natvie Tool Chain is enabled.
Please refer to webview official document, In addition, trusted JavaScript content in WebView can be allowed to directly access Windows RuntimeAPI. This provides powerful native capabilities for web apps hosted in WebView. To enable this feature, the Uniform Resource Identifier (URI) for trusted content must be added to the allowlist in the ApplicationContentUriRules of the app in Package.appxmanifest, with WindowsRuntimeAccess specifically set to "all".
For Xamarin solution, you need check if has add above allowlist for UWP project.