1

I'm attempting to run a WinExe with <UseWPF>true</UseWPF>, but it is not even running a simple Hello World program. I'm not sure what's wrong at this point, the only clue is this inner exception.

An unhandled exception of type 'System.BadImageFormatException' occurred in CSharpProgram.dll: 'Could not load file or assembly 'PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Reference assemblies cannot be loaded for execution. (0x80131058)'

Here is my .csproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
</Project>

Here is the program:

using System;

public class Program
{
    public static void Main() {
        Console.WriteLine("Hello World!");
    }
}

Any help is greatly appreciated.

2 Answers 2

1

I'm not familiar with Visual Studio Code, so the following isn't an answer for how to create a WPF Application in VS Code, but if desired, one can create a WPF Application using dotnet new .

To see installed SDKs:

  • dotnet --info

To see available templates

  • dotnet new list

To create a new WPF Application:

  • dotnet new wpf --framework <TargetFramework> --output <fully-qualified path>

see TargetFramework

For example, create a new project in a subfolder of your Documents folder (ex: %UserProfile%\Documents\Projects)

Note: Below you'll notice two subdirectories with the same name (WpfAppTest). The first one is for the solution and the second subfolder (with the same name) is for the project - this is the same structure Visual Studio creates so your solution/project will be usable by both Visual Studio Code and Visual Studio.

dotnet new wpf --framework net7.0 --output "%UserProfile%\Documents\Projects\WpfAppTest\WpfAppTest"

Create solution file:

Note: See dotnet sln for more information.

dotnet new sln --name "WpfAppTest" --output "%UserProfile%\Documents\Projects\WpfAppTest" --project "%UserProfile%\Documents\Projects\WpfAppTest\WpfAppTest\WpfAppTest.csproj"

Add Project to Solution File:

dotnet sln "%UserProfile%\Documents\Projects\WpfAppTest" add "%UserProfile%\Documents\Projects\WpfAppTest\WpfAppTest\WpfAppTest.csproj"

Optional - Add NuGet package

If desired, search for desired NuGet package, for example NuGet package Microsoft.Data.SqlClient.

Download and add desired NuGet package to project (ex: Microsoft.Data.SqlClient)

dotnet add "%UserProfile%\Documents\Projects\WpfAppTest\WpfAppTest\WpfAppTest.csproj" package "Microsoft.Data.SqlClient"

See dotnet add package for more information.


To Build Application:

dotnet build "%UserProfile%\Documents\Projects\WpfAppTest\WpfAppTest.sln"

To Run Application:

dotnet run --project "%UserProfile%\Documents\Projects\WpfAppTest\WpfAppTest\WpfAppTest.csproj"

Use your favorite editor to develop/modify the Console App. Both Visual Studio and Visual Studio Code are available here.

Sign up to request clarification or add additional context in comments.

1 Comment

Creating a new WPF Application worked for me. Thanks for the help!
0

From MSDN:

The UseWPF property controls whether or not to include references to WPF libraries. This also alters the MSBuild pipeline to correctly process a WPF project and related files. The default value is false. Set the UseWPF property to true to enable WPF support. You can only target the Windows platform when this property is enabled.

When this property is set to true, .NET 5+ projects will automatically import the .NET Desktop SDK.

So in behind, the compiler is automatically looking for WPF libraries and .NET Desktop SDK. You can try:

  1. Confirm if all the dependencies are installed (do you have all parts for that SDK)? Go to Visual Studio installer and check/add necessary parts
  2. Try to reference those projects/dependencies directly. Do you have all of them available and installed? If so, you could try to reference them directly from the project to make sure they are properly picked up.

When you create default WPF application without any changes, it looks like this (have those dependencies):

WPF default example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.