Repository navigation
Description
Current behavior 🐛
In a desktop head, C# that names the WebView2 control without its namespace doesn't compile:
using Microsoft.UI.Xaml.Controls;
namespace MyApp;
public sealed partial class MainPage : Page
{
private WebView2 CreateWebView() => new WebView2(); // error CS0118: 'WebView2' is a namespace but is used like a type
}Uno.UI.Runtime.Win32 depends on WebView2Aot through a plain PackageReference (Uno.UI.Runtime.Win32.csproj). The packed dependency hides only Build and Analyzers, so WebView2Aot's WebView2.dll is also available to the app at compile time.
That assembly declares its types under a root namespace named WebView2 (WebView2.Functions, WebView2.Utilities, ...). C# looks up a simple name in the global namespace before the compilation unit's using directives, so WebView2 resolves to that namespace instead of Microsoft.UI.Xaml.Controls.WebView2.
This affects every desktop head, with or without the webview UnoFeature, because every desktop executable references Uno.WinUI.Runtime.Win32. XAML is not affected, because the generated code uses global::-qualified names. The same C# compiles on WinUI.
Expected behavior 🎯
new WebView2() with using Microsoft.UI.Xaml.Controls; compiles and creates the control, as on WinUI. WebView2Aot is an implementation detail of the Win32 runtime: apps need its runtime asset, not its compile-time API.
How to reproduce it (as minimally and precisely as possible) 🔬
global.json:
{ "msbuild-sdks": { "Uno.Sdk": "7.0.0-dev.130" } }App.csproj:
<Project Sdk="Uno.Sdk">
<PropertyGroup>
<TargetFramework>net10.0-desktop</TargetFramework>
<OutputType>Exe</OutputType>
<UnoSingleProject>true</UnoSingleProject>
<UnoFeatures>Hosting</UnoFeatures>
</PropertyGroup>
</Project>Program.cs:
using Microsoft.UI.Xaml.Controls;
namespace MyApp;
public static class Program
{
public static void Main() => System.Console.WriteLine(new WebView2());
}Run dotnet build. It fails with error CS0118: 'WebView2' is a namespace but is used like a type, and obj/project.assets.json lists WebView2Aot/1.5.0. The result is the same with UnoFeatures set to Hosting;webview.
Workaround 🛠️
Either:
- fully qualify the type:
new Microsoft.UI.Xaml.Controls.WebView2(), or - put the
usinginside the namespace declaration, so it is looked up before the global namespace:namespace MyApp { using Microsoft.UI.Xaml.Controls; // new WebView2() compiles here }
Affected platforms 📱💻🖥️
Desktop (Windows), Desktop (macOS), Desktop (X11), Desktop (Linux Framebuffer)
Uno.Sdk version (and other relevant versions) 📦
Uno.Sdk 7.0.0-dev.130 (Uno.WinUI 7.0.0-dev.2538), WebView2Aot 1.5.0. Also present on master.
IDE version 🧑💻
dotnet CLI, .NET SDK 10.0.403
Anything else we need to know? 💬
A likely fix is to keep WebView2Aot out of consumers' compile references while still shipping its runtime asset. For example, in Uno.UI.Runtime.Win32.csproj:
<PackageReference Include="WebView2Aot" PrivateAssets="compile;contentfiles;analyzers;build" />Uno.UI.Runtime.Win32 still compiles against it, and apps still deploy WebView2.dll. This has not been verified yet.
Found while validating #25996.
Activity