jeudi 17 décembre 2020

How can I test a .net 5 WPF project using a console project in Visual Studio 2019?

I'm writing a simple tool used to modify file names of some specical files. I created a WPF project in .NET 5 in Visual Studio 2019 (version 16.8.3).

This project is created using the "WPF App(.NET)" template, and by right-clicking this project in Solution Explorer and choose "Properties", I set the "Target framework" to ".NET 5.0". The "Output type" is "Windows Application". The content of .csproj file of this WPF project:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
</Project>

In this WPF project, there are several classes I want to test. In the .NET framework age, we can simple create a new Console project in this solution, and reference the WPF project. The console project contains all test code. But, when I do this as usual, the problem arises:

Whether I choose the "Console App (.NET Core)" template or the "Console App (.NET Framework)" template (there are only these two templates for console application in Visual Studio 2019 (version 16.8.3)), the test project cannot reference the WPF project correctly. When build the solution, error occurs:

Project ... targets 'net5.0-windows'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v5.0'.

Or,

Project ... targets 'net5.0-windows'. It cannot be referenced by a project that targets '.NETFramework,Version=v4.7.2'.

What should I do?

Aucun commentaire:

Enregistrer un commentaire