-
Notifications
You must be signed in to change notification settings - Fork 678
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
WinUI3 - Unable to call method on host object with parameters #7170
Comments
There's a .Net IDispatch bug that's causing this not to work. There's a fix when using WebView2 in .Net, but it's not available for WinRT yet. Until it is, I suggest using WinRT code as described in the article Call native-side WinRT code from web-side code. |
@krschau I was able to get this working perfectly in a UWP app as specified by the documentation, however am having some trouble with a WinUI3 project. Any pointers? |
@rsegner Trying it out myself in WinUI 3, I'm not seeing the WebView2 properties page after installing WebView2 into the WinRTAdapter project. Is that what you're hitting? I'll ask the Edge folks about what's happening there. Edit: Actually, I updated to a newer WebView2 version and got around that issue. Now is it that the WinUI 3 project doesn't have a References section? In any case, I'm looking into it. |
@krschau any luck yet? I was able to create a WinUI3 C# class library referencing the Windows SDK along with WinRT, however in order to get the wv2winRT tool working I also needed to reference the preview of webview2. This causes a dependency error in the main application. |
@rsegner Sorry for the delay! I now have a working example: https://github.com/krschau/WinUI-WebView2-Samples/tree/master/NativeWebWinUI3App I'm going to write up actual instructions, and get it into the tutorial linked above, but I wanted to at least get the working example to you asap. If you look at the commit history, I broke the commits down by each step in the tutorial so you should be able to kind of follow along. The two last steps are the big departures from the WinUI 2 instructions: adding the cswinrt reference, and updating the csproj with CsWinrtInclude and updating to WinAppSDK 1.1. |
That's excellent news, thanks @krschau, I will give it a go |
Thanks @krschau, that is great, however, this still doesn't work for custom types. If I try to add a new windows runtime component to the solution and reference it in the WinRTAdapter, I get the following error: And If I try to add a simple class library that references the WinRTCS library I get: |
@rsegner I added a custom cppwinrt component to my example. There's a new section in the "Windows Runtime components with C++/WinRT" doc that was helpful, and then I also used part of this one. There's a little more detail in my project's README. Hopefully this helps. |
Thanks for the example @krschau. What I am wanting to do is create a WinUI3 class library that can reference the WindowsAppSDK and expose methods to the WebView. I have done a lot of experimentation around this and found that it may not be currently possible as the WinRT.Runtime.dll is referenced both by WindowsAppSDK and CSWinRT. I end up with an error like:
Is there a way I can create a C# WinUI3 class library containing objects that I can provide as a host object to webview2, which still has access to the APIs exposed by WindowsAppSDK? |
@krschau Please see this example repo: https://github.com/rsegner/WebViewWinRTTest Here I am trying to create a WinUI3 class library, referenced by a WinUI3 application including a WinRTAdapter. It builds successfully however I get a Note: this approach works with non-async methods, however, when I introduce an async method as in the example, the above error is thrown at runtime |
Describe the bug
When a host object is added to an instance of WebView2 as outlined in #4377 we are unable to call methods on the host object with parameters. The output to the JS console is
Error: The parameter is incorrect.
Steps to reproduce the bug
ICoreWebView2Interop
interface as outlined in AccessViolation 0xC0000005 was thrown while calling AddHostObjectToScript #4377Example Repo: https://github.com/rsegner/WinUi3WebViewTest/tree/main/WinUI3WebViewTest
Expected behavior
The method should return the expected result based on the input parameter
Screenshots
No response
NuGet package version
WinUI 3 - Windows App SDK 1.1 Preview 3: 1.1.0-preview3
Windows app type
Device form factor
No response
Windows version
Windows 10 (21H2): Build 19044
Additional context
No response
The text was updated successfully, but these errors were encountered: