WebView2Samples icon indicating copy to clipboard operation
WebView2Samples copied to clipboard

Calling C# native Winrt APIs from WebView2 in WinAppSDK

Open aaradhya1 opened this issue 2 years ago • 1 comments

I followed these links(https://learn.microsoft.com/en-us/microsoft-edge/webview2/how-to/winrt-from-js?tabs=winui2%2Cwinrtcsharp https://github.com/MicrosoftEdge/WebView2Samples/compare/uwp-wv2winrt-custom-csharp-winrt) and tried to create the same for WinUI3(WinAppSDK) instead of UWP.

As part of this, below steps are followed.

  1. Created a WinAppSDK winUI3 blank app(MyWinAppSDK)
  2. Created a C++ winRT(Universal Windows) component(NativeAdapter) and configured the wv2winrt tool
  3. Created a C# Winrt(UniversalWindows) library (Native Code) which has all my Native APIs

Here, NativeAdapter has a reference to NativeCode project which has C# APIs and MyWinAppSDK project has a reference to NativeAdapter which has the following properties set image

Finally when trying to build MyWinAppSDK project getting the below error:

NU1201 Project NativeCode is not compatible with net6.0-windows10.0.19041 (.NETCoreApp,Version=v6.0). Project NativeCode supports: uap10.0.17763 (UAP,Version=v10.0.17763)

Kindly guide me with the above issue and it would be helpful if there are any working samples for C# APIs getting called from WebView2 code in WinUI3?

aaradhya1 avatar Dec 04 '23 12:12 aaradhya1

Any Update I am facing the same issue, And also cannot add C# class library from Winui3 to Native Adapter

VishyBala007 avatar Apr 26 '25 18:04 VishyBala007