UWP Webview 2 WebViewContainer.CoreWebView2.AddHostObjectToScript without C++ WinRT Component

Vishy 0 Reputation points
2023-09-14T08:52:08.3433333+00:00

Hello All,

I have a UWP application where I am using WebView 2 Controls to host the websites.

Now when I need to connect between Js and my C# code all are saying to use the WinRT C++ adapter wrapper.

var dispatchAdapter = new WinRTAdapter.DispatchAdapter(); WebViewContainer.CoreWebView2.AddHostObjectToScript("external", dispatchAdapter.WrapObject(App.MessageCallWrapper, dispatchAdapter));

https://learn.microsoft.com/en-us/microsoft-edge/webview2/how-to/winrt-from-js?tabs=winui2%2Cwinrtcsharp

Is is necessary to use the WinRT C++ adapter project in UWP, without that I get error every-time and using it I am able to access the C# class from JS script.

Thanks,

Vishy

Universal Windows Platform (UWP)
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.