You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Finally add the following code to the CoreWebView2Initialized event from the WebView2 control from the main project:
usingCppWinRTComponent;privatevoidCoreWebView2Initialized(WebView2sender,CoreWebView2InitializedEventArgsargs){varfunctionHandler=newFunctionHandler();functionHandler.FunctionEvent+=(sender,payload)=>Debug.WriteLine($"Function called with the following payload: {payload}");vardispatchAdapter=newWinRTAdapter.DispatchAdapter();WebView.CoreWebView2.AddHostObjectToScript("functionHandler",dispatchAdapter.WrapObject(functionHandler,dispatchAdapter));}
To then call it from JavaScript using the following code:
As you can see, complex work is required to add a simple function, maybe this work can be simplified.
The scenario/use case where you would use this feature
I would use this feature in the above mentioned scenario
How important is this request to you?
Nice to have. There are other ways to tackle this, but having official API support would be beneficial.
Suggested implementation
A function called AddHostFunctionToScript could be created, so the implementation could look as simple as this:
usingCppWinRTComponent;privatevoidCoreWebView2Initialized(WebView2sender,CoreWebView2InitializedEventArgsargs){WebView.CoreWebView2.AddHostFunctionToScript("addedFunction",(stringpayload)=>Debug.WriteLine($"Function called with the following payload: {payload}"));}
To then call it from JavaScript using the following code:
Looping in @nishitha-burman from #3823 as well, as I think this ties into many of the other related issues/requests due to this #2754, #4521, #4664.
I've also just been trying to look at using AddHostObjectToScript from a C# in a WinUI 3 app, I was hoping I could just follow the WPF instructions or something, and surprised I'm having to follow a whole C++ project setup and all these other steps to glue two things together.
With the original WebView in UWP and AddWebAllowedObject this was literally one line and just passing in my C# object from my current project... it's oppressively complex right now to do this.
We'll look into this. We can do some work to improve docs and samples for the existing solution in the shorter term. To make something work like AddWebAllowedObject is a much larger feature that we can look at.
Describe the feature/enhancement you need
I would like to have a method similar to
AddHostObjectToScript
but for functions.Right now, to add simple functions (in my case using WinUI 3) I need to follow the steps from this guide and the Custom (3rd-party) WinRT components section from the same guide to have these projects:
FuntionHandler.idl
:FuntionHandler.h
:FuntionHandler.cpp
:Finally add the following code to the
CoreWebView2Initialized
event from the WebView2 control from the main project:To then call it from JavaScript using the following code:
As you can see, complex work is required to add a simple function, maybe this work can be simplified.
The scenario/use case where you would use this feature
I would use this feature in the above mentioned scenario
How important is this request to you?
Nice to have. There are other ways to tackle this, but having official API support would be beneficial.
Suggested implementation
A function called
AddHostFunctionToScript
could be created, so the implementation could look as simple as this:To then call it from JavaScript using the following code:
In .NET, it would need up to 34 method overloads due to the existence of 17
Func
delegate types and 17Action
delegate types.What does your app do? Is there a pending deadline for this request?
No response
The text was updated successfully, but these errors were encountered: