Skip to content
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

Closed
1 of 2 tasks
rsegner opened this issue May 31, 2022 · 10 comments
Closed
1 of 2 tasks

WinUI3 - Unable to call method on host object with parameters #7170

rsegner opened this issue May 31, 2022 · 10 comments
Labels
area-WebView bug Something isn't working product-winui3 WinUI 3 issues team-Rendering Issue for the Rendering team

Comments

@rsegner
Copy link

rsegner commented May 31, 2022

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

  1. Create a WinUI 3 project (either UWP or Desktop)
  2. Follow https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.addhostobjecttoscript
  3. Add host object using the ICoreWebView2Interop interface as outlined in AccessViolation 0xC0000005 was thrown while calling AddHostObjectToScript #4377
  4. Call a method on the host object, passing in a primitive type

Example 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

  • UWP
  • Win32

Device form factor

No response

Windows version

Windows 10 (21H2): Build 19044

Additional context

No response

@rsegner rsegner added the bug Something isn't working label May 31, 2022
@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label May 31, 2022
@krschau krschau added area-WebView team-Rendering Issue for the Rendering team product-winui3 WinUI 3 issues labels Jun 7, 2022
@krschau
Copy link
Contributor

krschau commented Jun 23, 2022

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.
That article focuses on OS provided APIs, to use your own WinRT APIs please see
MicrosoftEdge/WebView2Feedback#2529 (comment)

@krschau krschau closed this as completed Jun 23, 2022
@ghost ghost removed the needs-triage Issue needs to be triaged by the area owners label Jun 23, 2022
@rsegner
Copy link
Author

rsegner commented Jul 16, 2022

@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?

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Jul 16, 2022
@krschau
Copy link
Contributor

krschau commented Jul 18, 2022

@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.

@ghost ghost removed the needs-triage Issue needs to be triaged by the area owners label Jul 18, 2022
@rsegner
Copy link
Author

rsegner commented Jul 26, 2022

@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.

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Jul 26, 2022
@krschau
Copy link
Contributor

krschau commented Aug 3, 2022

@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.

@ghost ghost removed the needs-triage Issue needs to be triaged by the area owners label Aug 3, 2022
@rsegner
Copy link
Author

rsegner commented Aug 3, 2022

That's excellent news, thanks @krschau, I will give it a go

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Aug 3, 2022
@rsegner
Copy link
Author

rsegner commented Aug 4, 2022

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:
image

And If I try to add a simple class library that references the WinRTCS library I get:
image

@krschau
Copy link
Contributor

krschau commented Aug 15, 2022

@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.

@ghost ghost removed the needs-triage Issue needs to be triaged by the area owners label Aug 15, 2022
@rsegner
Copy link
Author

rsegner commented Aug 31, 2022

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:

Payload contains two or more files with the same destination path "WinRT.Runtime.dll

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?

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Aug 31, 2022
@rsegner
Copy link
Author

rsegner commented Sep 14, 2022

@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 Member not found at runtime.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-WebView bug Something isn't working product-winui3 WinUI 3 issues team-Rendering Issue for the Rendering team
Projects
None yet
Development

No branches or pull requests

3 participants