microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.35k stars 678 forks source link

Cannot create a new WinUI 3 app and target .NET 8 #9539

Closed wsficke closed 6 months ago

wsficke commented 7 months ago

SO X-Post: https://stackoverflow.com/questions/78317988/cannot-create-a-new-winui-3-app-and-target-net-8

Describe the bug

After creating a new WinUI 3 project using the project template, the framework version and nuget references are not current. In this state, targeting .NET 6, the app builds and runs, and you can see the clicky button screen.

The first order of business after executing any project template is to update to the latest framework and packages. With .NET 8, there are documented RID issues that are extern from this SDK. I found a workaround (UseRidGraph) on Stack Overflow to opt out of RID graph pruning, which lets the build pass.

Updating the nuget packages prior to targeting .NET 8 does not change the scenario. The error is introduced when retargeting to .NET 8, not when updating the nuget packages to current.

Build is passing with these props:

 <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
    <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
    <RootNamespace>Company.Product.Frontend.WinUI</RootNamespace>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <Platforms>x86;x64;ARM64;</Platforms>
    <RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
    <PublishProfile>win10-$(Platform).pubxml</PublishProfile>
    <UseRidGraph>true</UseRidGraph>
    <UseWinUI>true</UseWinUI>
    <EnableMsixTooling>true</EnableMsixTooling>
    <Nullable>enable</Nullable>
  </PropertyGroup>

However, when you start the app, you get this error:

Application: Company.Product.Frontend.WinUI.exe
CoreCLR Version: 8.0.424.16909
.NET Version: 8.0.4
Description: The process was terminated due to an unhandled exception.
Exception Info: System.DllNotFoundException: Unable to load DLL 'Microsoft.ui.xaml.dll' or one of its dependencies: The specified module could not be found. (0x8007007E)
   at Company.Product.Frontend.WinUI.Program.Main(String[] args) in C:\Users\me\source\repos\Company.Product\Company.Product.Frontend.WinUI\obj\x64\Release\net8.0-windows10.0.19041.0\win-x64\App.g.i.cs:line 28

Steps to reproduce the bug

  1. Install the SDK and create a new project as documented: https://learn.microsoft.com/en-us/windows/apps/winui/winui3/create-your-first-winui3-app
  2. Upgrade nuget packages to latest version, update target framework to latest version, opt out of RID graph pruning.
  3. Run the application and see the application event log for crash details.

Expected behavior

Windows Apps should run on .NET 8. The developer experience for starting a WinUI 3 project should be world-class and free of frustrating errors and workarounds.

Screenshots

System.DllNotFoundException: 'Unable to load DLL 'Microsoft.ui.xaml.dll' or one of its dependencies: The specified module could not be found. (0x8007007E)' @ App.g.i.cs

image

NuGet package version

WinUI 3 - Windows App SDK 1.5.2: 1.5.240404000

Windows version

Windows 11 (22H2): Build 22621

Additional context

Windows Apps should run great on .NET 8. The developer experience for starting a WinUI 3 project should be world-class and free of frustrating errors and workarounds.

Recommend maintaining the project templates to work with latest .NET and NuGet packages out of the box.

DarranRowe commented 6 months ago

That error is curious, it is also an error I do not see. Anyway, to go through the important points.

1) The projects being out of date is a known issue. The updated project templates that have .NET 8 support can be found here. You cannot build the .vsix file, but there is nothing stopping you from using these files to properly configure a new project. As an example to show this, there is a response to another bug report last month stating that the new project templates are on the way.

2) If you are using the Windows App SDK 1.5, you really shouldn't need UseRidGraph. A project that I created to test this out works perfectly fine with:

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
    <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
    <RootNamespace>CsTest</RootNamespace>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <Platforms>x86;x64;ARM64</Platforms>
    <RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
    <PublishProfile>win-$(Platform).pubxml</PublishProfile>
    <UseWinUI>true</UseWinUI>
    <EnableMsixTooling>true</EnableMsixTooling>
    <Nullable>enable</Nullable>
  </PropertyGroup>

If you want this to work with all frameworks, then using the method in the project templates will work.

3) The crash at XamlCheckProcessRequirements basically mean that you do not have the Windows App Runtime referenced. If your application is packaged, then you really need to provide a sample project that exhibits this behaviour since the build system should be picking up the reference from the NuGet package. If your application is unpackaged then you missed a step in the "Create your first WinUI 3 (Windows App SDK) project" link that you provided. As per step 7 in the unpackaged section, you need to set WindowsPackageType to none in the .csproj file. This will bootstrap the Windows App Runtime into the process and allow Xaml to work. There is an added annoyance here in that using WindowsPackageType set to none will break the packaging for this type of project. There are ways around it if you want the application to work seamlessly packaged or unpackaged.

wsficke commented 6 months ago

After rebooting the development machine (ad hoc, just to see if it would help), the project builds and runs on .NET 8 without issues. As others on the GitHub thread have indicated it is not replicable in their existing environments, it seems to me that maybe the documentation should say a restart is required, or the SDK installer should prompt for a reboot?

JesseCol commented 6 months ago

Glad to hear you're unblocked! If you come across more detail on what configuration change required a reboot, please let us know. thanks!

wsficke commented 6 months ago

It was a fresh installation of WinUI 3 SDK onto a Window 11 development machine with an existing Visual Studio 2022 environment.

JesseCol commented 6 months ago

Hi again, if anyone else hits this, you might also try uninstalling the WinAppSDK template extension from Visual Studio and then re-installing the latest. If you installed the standalone version from a past download link, it won't be updated in-place.

Thanks!

torum commented 2 months ago

Hi! I just encounter the same issue here. Rebooting does not help. I used @TemplateStudio to create a fresh simple app. I then changed "win10-x64" to "win-x64". Here is what I got.

スクリーンショット 2024-08-17 110002