首页 > 解决方案 > .Net 5 - WPF, unit test woes

问题描述

I'm having some issues while playing around with .Net 5, so I'm hoping someone could shed some light on things. Having worked on a large enterprise .Net Framework solution for a number of years, .Net Core seems to have passed me by and I've had no exposure to it, which might explain some of my confusion...

Unit test references?

I have created a solution containing a WPF project ("WPF App (.NET)"), and a unit test project ("MSTest Test project (.Net Core)"). Both have been set to a target framework of ".NET 5" in their properties page.

When I reference the WPF project from the unit test project, a yellow triangle appears alongside the project reference, and the following error in the Error List window:

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

How on earth do I reference the WPF project from the UT project?

Class lib referencing WPF types?

I have also created a .Net 5 class library, which needs to reference various WPF types (controls, etc), but I'm not sure how to configure the necessary assembly/framework references (in the Framework world I'd simply add assembly references to PresentationFramework, System.Xaml, etc). Somehow, through trial and error I did get this working, using a combination of these lines in the project file:-

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<UseWPF>true</UseWPF>

as well as changing the target framework from "net5.0" to "net5.0-windows".

However I'm not sure which combination of these are actually required, or if I'm even supposed to be editing the project file by hand, which feels like a step backwards if it can't be done via Visual Studio!

标签: c#.netwpf.net-core.net-5

解决方案


从 .NET 5.0 开始,为 WPF 和 Windows 窗体创建新项目与 .NET Core 3.0 和 3.1 中的不同。

从 .NET 5.0 及更高版本开始,为 WPF 和 Windows 窗体创建新项目需要具有特定于操作系统的目标框架名称,以及项目文件中支持的语言的 SDK 标头,例如 csproj (C#)、vbproj (VB)、fsproj (F#)使用与类库和控制台应用程序相同的 SDK 名称。

因此,在 .NET 5.0 及更高版本中,您不必 Sdk="Microsoft.NET.Sdk.WindowsDesktop"为任何使用 WPF 和 Windows 窗体的项目指定,但必须根据需要显式<UseWPF>指定<UseWindowsForms>

例如,这是一个基本的 WinForms 可执行项目:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows7.0</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

</Project>

对于引用 Windows 窗体的单元测试项目,您可以像以下示例一样更新您的单元测试项目:(注意附加<UseWindowsForms>true</UseWindowsForms>元素)

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0-windows</TargetFramework>
    <IsPackable>false</IsPackable>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

  <ItemGroup>
      <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
      <PackageReference Include="MSTest.TestAdapter" Version="2.2.4" />
      <PackageReference Include="MSTest.TestFramework" Version="2.2.4" />
      <PackageReference Include="coverlet.collector" Version="1.3.0" />
  </ItemGroup>

</Project>

推荐阅读