-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Enhancement] (WebView) - Expose C# classes in JS #9424
Comments
Thank you so much for the suggestion @TGSAN! I don't know a lot about this area, but I see a lot of COM stuff, which immediately makes me think this is very specific to Windows? So this will only work on Windows? |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
No, it can be implemented on any platform. Currently only WebView2 (Windows) provides this API. but this API design can be available on any platform (e.g. iOS and Android). The specific implementation can be achieved through the WebView of different platforms. Register a |
Both Android WebView and iOS WKWebView implement this similar functionality, but not the Windows version, making this a stopper to any advanced task in MAUI+WebView. And this functionality works in other Windows platforms like WPF or Windows Forms... All these are community hacks: But all these are far from ideal, and most of these force us to work with WinRT adapters. Just look this article:
EDIT: I got now that it is a "Request to add AddHostObjectToScript to Maui WebView", that currently does not have. But as I said, the only stopper is the Windows WebView (CoreWebView2), which does not correctly implement this. Regards |
As of .NET 9 we will have the HybridWebView, will that help in this scenario? See #22880 |
Description
Add
AddHostObjectToScript
to MAUI's WebView so that the same code can be used to implement JSBridge on any platformThe API design can be found in the Microsoft WebView2 documentation:
https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.addhostobjecttoscript
This feature is proposed to enhance the flexibility of calls between Javascript code and C# code in WebView.
It is possible to expose specific C# classes for Javascript code in WebView, so that Javascript code can call members on the classes (for notifications or transferring data). Previously using MAUI required developers to write duplicate code for each platform to implement similar functionality (a capability available in WebView implementations on various platforms), when this feature is implemented in MAUI, developers can focus solely on writing MAUI common code, lowering the barrier to use.
Public API Changes
In C#
In C# (Then add instances of those classes via
AddHostObjectToScript(String, Object)
)In Javascript (C# class as a Javascript proxy object)
Intended Use-Case
In my project, I want to reuse some binary data processing libraries written in Javascript, and then notify and give the processed binary data to C# for processing. As another example, there are many HyperApps, that need to notify the C# side after the Javascript initialization on the web side is finished (e.g. the login is done in the WebView and then the Token is passed to the App).
The text was updated successfully, but these errors were encountered: