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

WinUI: WebView2 does not display in UnoSdk project #18552

Open
baskren opened this issue Oct 23, 2024 · 14 comments · Fixed by #18872
Open

WinUI: WebView2 does not display in UnoSdk project #18552

baskren opened this issue Oct 23, 2024 · 14 comments · Fixed by #18872
Assignees
Labels
area/webview Categorizes an issue or PR as relevant to the WebView control difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification

Comments

@baskren
Copy link
Contributor

baskren commented Oct 23, 2024

Current behavior

Two WinUI3 apps:

The plain WinUI3 app will render a WebView2 but the UnoSdk app will not.

Expected behavior

WebView2 works with in Uno.Sdk projects when targeting WinUI

How to reproduce it (as minimally and precisely as possible)

Expected Behavior: https://github.com/baskren/WinUi3WebView2.git
Current Behavior: https://github.com/baskren/UnoSdkWinuiWebView2.git

Workaround

None known at this time

Works on UWP/WinUI

Yes

Environment

Uno.WinUI / Uno.WinUI.WebAssembly / Uno.WinUI.Skia

NuGet package version(s)

Expected Behavior:
image

Current Behavior:
"Uno.Sdk": "5.4.10"
image

Affected platforms

Windows (WinAppSDK)

IDE

No response

IDE version

Microsoft Visual Studio Community 2022 (ARM 64-bit) - Current Version 17.11.5

Relevant plugins

VisualStudio Installed Products:

Microsoft Visual Studio Community 2022
Version 17.11.5
VisualStudio.17.Release/17.11.5+35327.3
Microsoft .NET Framework
Version 4.8.09032

Installed Version: Community

Visual C++ 2022 00482-90000-00000-AA155
Microsoft Visual C++ 2022

ASP.NET and Web Tools 17.11.231.19466
ASP.NET and Web Tools

AvaloniaPackage Extension 1.0
AvaloniaPackage Visual Studio Extension Detailed Info

Azure App Service Tools v3.0.0 17.11.231.19466
Azure App Service Tools v3.0.0

Azure Functions and Web Jobs Tools 17.11.231.19466
Azure Functions and Web Jobs Tools

C# Tools 4.11.0-3.24460.3+5649376e0e5f5db3743a94a62b073f2cce4be5d9
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

CleanBinAndObjCommand Extension 1.2.58
CleanBinAndObjCommand Visual Studio Extension Detailed Info

Extensibility Message Bus 1.4.39 (main@e8108eb)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

GitHub Copilot 0.2.1657.32929
GitHub Copilot is an AI pair programmer that helps you write code faster and with less work.

ILSpy.AddIn 1.0
Integration of the ILSpy Decompiler into Visual Studio.

Microsoft JVM Debugger 1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Node.js Tools 1.5.60329.1 Commit Hash:15fce86a17f9f33e5bd7ca7ccb9b216aff149252
Adds support for developing and debugging Node.js apps in Visual Studio

NuGet Package Manager 6.11.1
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

Project System Tools 1.0
Tools for working with C#, VisualBasic, and F# projects.

Razor (ASP.NET Core) 17.11.3.2442001+68650a7d94261bc56a1f4bc522c2ee35314b1abb
Provides languages services for ASP.NET Core Razor.

SQL Server Data Tools 17.11.47.0
Microsoft SQL Server Data Tools

TypeScript Tools 17.0.30715.2002
TypeScript Tools for Microsoft Visual Studio

Uno Platform Extension 1.0
Uno Platform Extension to create cross-platform apps using iOS, Android, mac Catalyst, Skia and WebAssembly.

Visual Basic Tools 4.11.0-3.24460.3+5649376e0e5f5db3743a94a62b073f2cce4be5d9
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools 17.11.0-beta.24421.7+af2f522de602281d4ef5a7b71507c428e814c5c1
Microsoft Visual F# Tools

Visual Studio IntelliCode 2.2
AI-assisted development for Visual Studio.

VisualStudio.DeviceLog 1.0
Information about my package

VisualStudio.Mac 1.0
Mac Extension for Visual Studio

VSPackage Extension 1.0
VSPackage Visual Studio Extension Detailed Info

Xamarin 17.11.0.98 (d17-11@86652fe)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer 17.11.3.11 (remotes/origin/d17-11@cdbb0a4fdd)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin.Android SDK 13.2.2.0 (d17-5/45b0e14)
Xamarin.Android Reference Assemblies and MSBuild support.
Mono: d9a6e87
Java.Interop: xamarin/java.interop/d17-5@149d70fe
SQLite: xamarin/sqlite@68c69d8
Xamarin.Android Tools: xamarin/xamarin-android-tools/d17-5@ca1552d

Xamarin.iOS and Xamarin.Mac SDK 16.4.0.23 (9defd91b3)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

Anything else we need to know?

No response

@baskren baskren added difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification labels Oct 23, 2024
@MartinZikmund MartinZikmund self-assigned this Oct 23, 2024
@MartinZikmund MartinZikmund added the area/webview Categorizes an issue or PR as relevant to the WebView control label Oct 23, 2024
@baskren
Copy link
Contributor Author

baskren commented Oct 25, 2024

Just some quick testing ... using the below Uno.Templates versions, does a new single project solution with the following MainPage.xaml render the WebView2?

<Page x:Class="UnoWebViewTest.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:UnoWebViewTest"
      Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="40" />
      <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <TextBlock AutomationProperties.AutomationId="HelloTextBlock"
          Text="Hello Uno Platform!"
          HorizontalAlignment="Center"
          VerticalAlignment="Center" />
    <WebView2
          x:Name="MyWebView"
          Grid.Row="1"
          DefaultBackgroundColor="Pink"
          Source="https://www.microsoft.com"
          HorizontalAlignment="Stretch"
          VerticalAlignment="Stretch"
          />
  </Grid>
</Page>

Uno.Templates Version WebView2 rendered?
5.3.31 YES
5.3.112 YES
5.4.0-dev.2* image
5.4.0-dev.97 YES
5.4.0-dev.182 YES
5.4.0-dev.200 YES
5.4.0-dev.220 YES
5.4.0-dev.230 YES
5.4.0-dev.236 YES
5.4.0-dev.261 NO
5.4.5 NO
5.6.0-dev.43 NO

* Configuration manager settings:
image

@baskren
Copy link
Contributor Author

baskren commented Oct 25, 2024

Differences found between Uno.Template / Uno.Sdk single project apps:

  • References:
Version References
Uno.Sdk 5.4.0-dev.236 image
Uno.Sdk 5.4.0-dev.261 image

@MartinZikmund
Copy link
Member

@baskren thank you for the extensive investigation, it really helped!

I have a workaround now which is to add additional processor architectures in Configuration Manager:

Image

When building directly against x64, etc. WebView2 shows up correctly. We will now need to understand how to adjust Sdk to handle this properly for "AnyCPU"

@baskren
Copy link
Contributor Author

baskren commented Oct 25, 2024

@MartinZikmund : Thank you for giving this some attention. I'm trying to see if I can get the suggested work-around to work. When I go to the configuration manager, I see that I already have additional processor architectures:

image

When I select just one (ex: Arm64 or X64), the WebView2 still does not appear.

Given that you've a better understanding of the mechanics here, do you have a suggestion for me?

To answer my own question on how to fix this if there are other platforms existing in the Configuration Manager:

  • Launch Configuration Manager
  • In Active solution platform: dropdown, select <Edit ..>
  • Delete all platforms other than Any CPU
  • Restart VisualStudio
  • Per Martin's recommendation, create a new solution platform using the Configuration Manager

@baskren
Copy link
Contributor Author

baskren commented Oct 25, 2024

CORRECTION: The above comment was for the 5.4.0-dev.261 version. Your suggested work-around works great for 5.4.10!

@MartinZikmund
Copy link
Member

@jeromelaban I was investigating this previously, but so far unable to figure out the difference relevant to WebView2 between binlogs for plain WinAppSDK app and Uno.Sdk app. For AnyCPU we are overriding the relevant runtime identifier in the SDK so technically it should not be a problem to have AnyCPU selected, but somehow it still matters in case of WebView2 and the required dependencies are not deployed along with the app. Can you check this if you have a chance?

@jeromelaban
Copy link
Member

My guess is that it is related to the decoupling of WebView 2:

Decoupled WebView2 versioning
Instead of embedding a hard-coded version of the Edge WebView2 SDK into the Windows App SDK, we now consume the Edge WebView2 SDK as a NuGet reference in 1.6. If your app uses WebView2 to display content, you can now choose a newer version of the Microsoft.Web.WebView2 package if you need to instead of the version that came with the Windows App SDK. Additionally, you can now reference NuGet packages which also reference the Edge WebView2 SDK, streamlining your dependencies.
With these updates, we’ve decoupled the WinAppSDK from the Edge WebView2 API surface and brought NuGet’s sophisticated package management capabilities to the WebView2 space. Your WebView2-powered app experiences are now more versatile than ever and can tap into the newest WebView2 offerings at any time!

My guess would be that adding:

    <PackageVersion Include="Microsoft.Web.WebView2" Version="1.0.2950-prerelease" />

should fix the problem, but so far, adding the webview2 package crashes the app.

@MartinZikmund
Copy link
Member

@jeromelaban Based on the fact this workaround helps it seems that the AnyCPU is the problem, just not sure why the overrides Uno.Sdk does don't help:

@baskren thank you for the extensive investigation, it really helped!

I have a workaround now which is to add additional processor architectures in Configuration Manager:

Image

When building directly against x64, etc. WebView2 shows up correctly. We will now need to understand how to adjust Sdk to handle this properly for "AnyCPU"

@jeromelaban
Copy link
Member

A tentative fix is present in #18872, but in the meantime, adding this block in the csproj will help:

<PropertyGroup>
   <!-- Workaround for https://github.com/unoplatform/uno/issues/18552 -->
   <PlatformTarget Condition=" '$(PlatformTarget)' == '' AND $(Platform) == 'AnyCPU' AND '$(NETCoreSdkRuntimeIdentifier)' == 'win-x86'">x86</PlatformTarget>
   <PlatformTarget Condition=" '$(PlatformTarget)' == '' AND $(Platform) == 'AnyCPU' AND '$(NETCoreSdkRuntimeIdentifier)' == 'win-x64'">x64</PlatformTarget>
   <PlatformTarget Condition=" '$(PlatformTarget)' == '' AND $(Platform) == 'AnyCPU' AND '$(NETCoreSdkRuntimeIdentifier)' == 'win-arm64'">arm64</PlatformTarget>
</PropertyGroup>

@MartinZikmund
Copy link
Member

MartinZikmund commented Dec 3, 2024

It seems that even with the latest version of Uno.Sdk this is not working yet somehow 🤔 . The provided workaround above works however

image

MartinZikmund added a commit to MartinZikmund/mzikmund that referenced this issue Dec 3, 2024
@jeromelaban
Copy link
Member

jeromelaban commented Dec 3, 2024

The issue is fixed in latest dev (the workaround is included).

MartinZikmund added a commit to MartinZikmund/mzikmund that referenced this issue Dec 4, 2024
@MartinZikmund MartinZikmund reopened this Dec 4, 2024
@MartinZikmund
Copy link
Member

I want to verify this once more, I was using latest dev from yesterday, so it should have been there already 🤔

@MartinZikmund
Copy link
Member

MartinZikmund commented Dec 4, 2024

@jeromelaban I tested now with the latest 5.6.0-dev.264 and still the same result: %1 is not a valid Win32 application

Applying the workaround directly in .csproj works, so maybe the Sdk workaround is just applied too late 🤔 ?

Repro:

  1. Blank Uno app
  2. XAML:
<StackPanel
    HorizontalAlignment="Center"
    VerticalAlignment="Center">
    <WebView2 x:Name="Test" />
</StackPanel>
  1. Code-behind:
public MainPage()
{
    this.InitializeComponent();
}

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    await Test.EnsureCoreWebView2Async();
    Test.CoreWebView2.Navigate("https://platform.uno");
}

@jeromelaban
Copy link
Member

This feels like a different issue, but yes, maybe it needs to be applied earlier then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/webview Categorizes an issue or PR as relevant to the WebView control difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants