Repository navigation
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a NativeAOT-compatible WebView2 backend for the Win32 Skia runtime by introducing a WebView2Aot-based implementation for .NET 10+ while keeping the existing Microsoft.Web.WebView2 backend available (and the only option on .NET 9). This targets the Native AOT COM interop limitation reported in #23142.
Changes:
- Split Win32 HWND/WndProc hosting logic into a new shared
Win32NativeWebViewBasefor reuse by multiple WebView2 backends. - Added
Win32NativeAotWebView(NET10+) implemented viaWebView2Aot, and updated backend selection viaUNO_WEBVIEW2_BACKEND. - Updated packaging/build: conditional
WebView2Aotreference (NET10+), added BannedApiAnalyzers +BannedSymbols.txt, and centralizedWebView2Aotversion.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/Uno.UI.Runtime.Skia.Win32/Uno.UI.Runtime.Skia.Win32.csproj | Adds conditional WebView2Aot dependency (NET10+), enables BannedApiAnalyzers, and includes BannedSymbols.txt as AdditionalFiles. |
| src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeWebViewBase.cs | New shared base for Win32 child window creation/WndProc forwarding and resize handling. |
| src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeWebView.cs | Updates provider to select backend via env var and makes the existing backend inherit the new base class. |
| src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeAotWebView.cs | New NET10+ WebView2Aot-based backend (AOT-compatible COM interop path). |
| src/Uno.UI.Runtime.Skia.Win32/BannedSymbols.txt | Bans DirectN.PWSTR.From(string) usage with guidance for safe alternatives. |
| src/Directory.Build.targets | Pins WebView2Aot package version centrally. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-23604/wasm-skia-net9/index.html |
|
The build 220289 did not find any UI Test snapshots differences. |
|
|
|
|
0ed4f5d to
72e65b9
Compare
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-23604/wasm-skia-net9/index.html |
|
The build 220340 did not find any UI Test snapshots differences. |
|
Tick the box to add this pull request to the merge queue (same as
|
| <PackageReference Update="CommunityToolkit.Mvvm" Version="$(CommunityToolkitMvvmVersion)" /> | ||
| <PackageReference Update="System.CommandLine" Version="2.0.0-beta4.22272.1" /> | ||
| <PackageReference Update="Microsoft.Web.WebView2" Version="1.0.2592.51" Condition="'$(MSBuildProjectName)' != 'Uno.WinAppSDKSyncGenerator.References'" /> | ||
| <PackageReference Update="WebView2Aot" Version="1.5.0" /> |
There was a problem hiding this comment.
The new package will need to be added to Uno.Sdk and uno.templates
There was a problem hiding this comment.
@MartinZikmund: I don't understand "added to Uno.Sdk and uno.templates".
A .nuspec is generated for Uno.UI.Runtime.Skia.Win32 in Release builds: https://github.com/unoplatform/uno/blob/feature/breakingchanges/src/Uno.UI.Runtime.Skia.Win32/Uno.UI.Runtime.Skia.Win32.csproj#L10
<GeneratePackageOnBuild Condition="'$(Configuration)'=='Release'">true</GeneratePackageOnBuild>Indeed, when I build locally, the resulting generated obj/Release/Uno.WinUI.Runtime.Skia.Win32.255.255.255.255.nuspec contains:
<group targetFramework="net10.0">
<dependency id="Uno.WinUI.Runtime.Skia" version="255.255.255.255" exclude="Build,Analyzers" />
<dependency id="Uno.WinUI" version="255.255.255.255" exclude="Build,Analyzers" />
<dependency id="HarfBuzzSharp" version="8.3.1.1" exclude="Build,Analyzers" />
<dependency id="Microsoft.Windows.CsWin32" version="0.3.106" exclude="Build,Analyzers" />
<dependency id="Uno.icu-win" version="77.2.1" exclude="Build,Analyzers" />
<dependency id="WebView2Aot" version="1.5.0" exclude="Build,Analyzers" />
</group>Is more needed?
Additionally, what needs to be done in uno.templates? Is there an example to copy?
There was a problem hiding this comment.
I was under the impression that there was a prop to override WebView2 version, but it seems there is not currently. In that case we can probably skip that for now, maybe it will not be necessary then.
There was a problem hiding this comment.
@jonpryor however, because the environment variable decides whether the AOT or non-AOT version is used, does it mean we now bundle both versions? In that case, it would probably be best to default to AOT version and have a prop in Uno.Sdk that would allow people, if they chose to, to switch to the non-AOT one, so that the choice happens before build, instead of having the option to switch at runtime
There was a problem hiding this comment.
does it mean we now bundle both versions?
Yes.
it would probably be best to default to AOT version
That is the default, which is also mentioned in the updated WebView docs: https://github.com/unoplatform/uno/blob/dev/jonpryor/jonp-Windows%2BNativeAOT%2BWebView2/doc/articles/controls/WebView.md#windows-specifics
instead of having the option to switch at runtime
The benefit to switching at runtime via environment variable is with testing: you can build once, then test either/both backends without requiring an app rebuild. (You would need to relaunch the app if setting the env var from the launching Console, but this is required anyway…)
The other benefit to the env var approach is that it adds no new public API, so we can change things without API breaks in the future.
Additionally, my hope is that for Uno 8 we drop the Microsoft.Web.WebView2 backend entirely, and use only WebView2Aot.
There was a problem hiding this comment.
Runtime switching is nice for us, but I think for end users it will mean bigger assembly size, which is not ideal and is probably more important than the convenience... So if it is possible, it would be best to make sure only the selected version is bundled, via Uno.Sdk. As long as WebView2Aot seems stable, we can just keep it default and in case someone needs to, they could set some .csproj prop to swap for the old backend.
Fixes: #23142 Context: MicrosoftEdge/WebView2Feedback#5238 Context: https://github.com/smourier/WebView2Aot Context: smourier/DirectNAot#9 On Windows, the <WebView2/> XAML control is implemented in terms of the [Microsoft.Web.WebView2 NuGet package][0]. The problem: Microsoft.Web.WebView2, when used for "normal" .NET, uses COM Interop. Native AOT does not support COM interop. The result is that things fail, a'la MicrosoftEdge/WebView2Feedback#5238 (which is for WinForms, but the same fundamental idea). In the case of #23142, if we build Uno.Gallery for Native AOT: git clone https://github.com/unoplatform/Uno.Gallery.git cd Uno.Gallery git checkout 54a08b1587aa020a5651d61eb3a3023a79e89d72 sed -i '' 's/"Uno.Sdk": ".*"/"Uno.Sdk": "6.6.0-dev.216"/g' global.json dotnet publish -c Release -r win-arm64 -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop ` -bl Uno.Gallery/Uno.Gallery.csproj -p:SelfContained=true -p:UseSkiaRendering=true ` -p:SkiaPublishAot=true -p:IsAotCompatible=true Run it: .\Uno.Gallery\bin\Release\net10.0-desktop\win-arm64\publish\Uno.Gallery.exe | Out-Default and access the **UI components > WebView** entry in the left tree, then the console contains the following output: fail: Uno.UI.Dispatching.NativeDispatcher[0] NativeDispatcher unhandled exception System.NotSupportedException: COM Interop requires ComWrapper instance registered for marshalling. at System.Runtime.InteropServices.ComWrappers.ComInterfaceForObject(Object) + 0xa4 at System.Runtime.InteropServices.ComWrappers.ComInterfaceForObject(Object, Guid) + 0x24 at Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateCoreWebView2EnvironmentWithOptions(String, String, ICoreWebView2EnvironmentOptions, ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler) + 0xdc at Microsoft.Web.WebView2.Core.CoreWebView2Environment.<CreateAsync>d__102.MoveNext() + 0xe4 --- End of stack trace from previous location --- at Uno.UI.Runtime.Skia.Win32.Win32NativeWebView.<>c__DisplayClass15_0.<<-ctor>b__0>d.MoveNext() + 0x60 --- End of stack trace from previous location --- at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__124_0(Object state) + 0x1c at Uno.UI.Dispatching.NativeDispatcher.RunAction(NativeDispatcher, Action) + 0x8c The Microsoft.Web.WebView2 package *does* support Native AOT, but *only if* you're building for WinRT. (Uno is not built on WinRT.) A way to use Microsoft.Web.WebView2 while supporting Native AOT in a "normal" (non-WinRT) Windows environment is to instead use smourier/WebView2Aot, in the [WebView2Aot NuGet package][1]. WebView2Aot uses the [DirectN AOT generator][2] to emit Native AOT- compatible COM interop infrastrucure. Update `Uno.UI.Runtime.Skia.Win32` as follows: 1. Split out the "native" bits of `Win32NativeWebView` into `Win32NativeWebViewBase.cs`. (Anything dealing with a Windows WndProc, etc.) 2. Update `Win32NativeWebView` to inherit `Win32NativeWebViewBase`. 3. Add a new `Win32NativeAotWebView` type, which also inherits from `Win32NativeWebViewBase`. This implements `<WebView2/>` support in terms of WebView2Aot. However, WebView2Aot requires .NET 10. Conseuqently, `Uno.UI.Runtime.Skia.Win32.csproj` should only reference WebView2Aot when targeting .NET 10 and later, and `Win32NativeAotWebView` can only exist in .NET 10 and later. Additionally, this is a potential significant change. We want WebView2Aot to be the default in all Windows configurations for consistency -- nobody wants to debug a scenario of "WebView2 works when I'm not building for Native AOT!" -- but there should be a way to use the previous Microsoft.Web.WebView2 implementation in case WebView2Aot breaks things, or for testing, or… Update `Win32NativeWebViewProvider` as follows: 1. When built for .NET 9, only Win32NativerWebView / the Microsoft.Web.WebView2 backend is supported. 2. When built for .NET 10+, the new WebView2Aot backend is used by default. This can be overridden by setting the `UNO_WEBVIEW2_BACKEND` environment variable to `microsoft.web.webview2`. `UNO_WEBVIEW2_BACKEND` can also be set to `webview2aot` to be explicit. The `UNO_WEBVIEW2_BACKEND` environment variable is always read, and will log an error message when using an unsupported value, e.g. when `UNO_WEBVIEW2_BACKEND=webview2aot` on .NET 9. Aside: add BannedApiAnalyzers to `Uno.UI.Runtime.Skia.Win32.csproj` which make it an error to use `PWSTR.From(string)`. See also smourier/DirectNAot#9. [0]: https://www.nuget.org/packages/Microsoft.Web.WebView2 [1]: https://www.nuget.org/packages/WebView2Aot [2]: https://github.com/smourier/DirectNAot
72e65b9 to
3fb1fc8
Compare
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-23604/wasm-skia-net9/index.html |
|
The build 220487 did not find any UI Test snapshots differences. |
Fixes: #23142
Context: MicrosoftEdge/WebView2Feedback#5238
Context: https://github.com/smourier/WebView2Aot
Context: smourier/DirectNAot#9
On Windows, the XAML control is implemented in terms of the Microsoft.Web.WebView2 NuGet package.
The problem: Microsoft.Web.WebView2, when used for "normal" .NET, uses COM Interop. Native AOT does not support COM interop. The result is that things fail, a'la
MicrosoftEdge/WebView2Feedback#5238 (which is for WinForms, but the same fundamental idea).
In the case of #23142, if we build Uno.Gallery for Native AOT:
Run it:
and access the UI components > WebView entry in the left tree, then the console contains the following output:
The Microsoft.Web.WebView2 package does support Native AOT, but only if you're building for WinRT. (Uno is not built on WinRT.)
A way to use Microsoft.Web.WebView2 while supporting Native AOT in a "normal" (non-WinRT) Windows environment is to instead use smourier/WebView2Aot, in the WebView2Aot NuGet package. WebView2Aot uses the DirectN AOT generator to emit Native AOT- compatible COM interop infrastrucure.
Update
Uno.UI.Runtime.Skia.Win32as follows:Split out the "native" bits of
Win32NativeWebViewintoWin32NativeWebViewBase.cs. (Anything dealing with a Windows WndProc, etc.)Update
Win32NativeWebViewto inheritWin32NativeWebViewBase.Add a new
Win32NativeAotWebViewtype, which also inherits fromWin32NativeWebViewBase. This implements<WebView2/>support in terms of WebView2Aot.However, WebView2Aot requires .NET 10. Conseuqently,
Uno.UI.Runtime.Skia.Win32.csprojshould only reference WebView2Aot when targeting .NET 10 and later, andWin32NativeAotWebViewcan only exist in .NET 10 and later.Additionally, this is a potential significant change. We want WebView2Aot to be the default in all Windows configurations for consistency -- nobody wants to debug a scenario of "WebView2 works when I'm not building for Native AOT!" -- but there should be a way to use the previous Microsoft.Web.WebView2 implementation in case WebView2Aot breaks things, or for testing, or…
Update
Win32NativeWebViewProvideras follows:When built for .NET 9, only Win32NativerWebView / the Microsoft.Web.WebView2 backend is supported.
When built for .NET 10+, the new WebView2Aot backend is used by default. This can be overridden by setting the
UNO_WEBVIEW2_BACKENDenvironment variable tomicrosoft.web.webview2.UNO_WEBVIEW2_BACKENDcan also be set towebview2aotto be explicit.The
UNO_WEBVIEW2_BACKENDenvironment variable is always read, and will log an error message when using an unsupported value, e.g. whenUNO_WEBVIEW2_BACKEND=webview2aoton .NET 9.Aside: add BannedApiAnalyzers to
Uno.UI.Runtime.Skia.Win32.csprojwhich make it an error to usePWSTR.From(string). See also smourier/DirectNAot#9.PR Type:
🐞 Bugfix
✨ Feature
What changed? 🚀
PR Checklist ✅
Screenshots Compare Test Runresults.