How to Invoke a JS function on a JS object from native code #3982
Replies: 6 comments
-
Simply post message from web side code and add an event handler in native code. Then you can execute script based on the message. See ex. https://docs.microsoft.com/en-us/microsoft-edge/webview2/how-to/communicate-btwn-web-native#round-trip-messages. Let me know if this doesn't work for your scenario. |
Beta Was this translation helpful? Give feedback.
-
@jasonstephen15 , Thanks but that's not really what I'm asking about it, what I want is be able to call a function on a JS object. |
Beta Was this translation helpful? Give feedback.
-
By the way, I don't understand why Microsoft chooses to make webView examples in c++, which is not really the most used language at the moment, by far c# is. and much more readable |
Beta Was this translation helpful? Give feedback.
-
@jasonstephen15 , Please check this interface in MS Blazor, which does very similar to what I'm talking about |
Beta Was this translation helpful? Give feedback.
-
@ahmed-salem-me - We currently don't have a way to project JS objects into your native language. I believe Blazor is only C#/.NET whereas WebView2 has multiple language/platform support. This would probably be costly for us to build as we'd have to handle multiple native language/interface projections. I do agree it might be slightly easier to have a native proxy for the js object. Although I don't know if I see a functionality gap. Do you mind explaining if there is some feature/scenario gap you can't accomplish with ExecuteScript? In your example above, would webView2.ExecuteScriptAsync("getElement(id).focus()") not meet your needs? |
Beta Was this translation helpful? Give feedback.
-
@jasonstephen15 , the main difference is that you can pass the object around, so you can call its functions later or set its attributes. It's not that you project the entire JS object into native code as it is, but rather some kind of reference to it, that allow for future operations on that object, called from native and executed on the web-side |
Beta Was this translation helpful? Give feedback.
-
How can web code return a js object to native code which native code can later use to invoke a function of the js object?
Ex.
var obj = newObject();
var obj.DoSomething = function (){};
function GetObject() {
return obj;
}
var tmp = {NativeCode}.ExecuteJavascript("GetObject()");
tmp.{SomeHow}.Call(DoSomething());
Beta Was this translation helpful? Give feedback.
All reactions