c# - .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!
解决方案
从 .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>
推荐阅读
- angular - 我们如何在Angular 6的多个选择列表中动态选择一些选项
- oracle-ebs - oracle r12中pa_projects_all表中project_id和segment1的序列是什么
- twitter-bootstrap - 如何让我的下拉菜单在容器内部和左侧打开
- php - 如何在 CodeIgniter PHP 中优化发送推送通知?
- android - 活动堆栈未清除
- javascript - 猫头鹰轮播打破了引导模式
- c++ - 如何在 Visual Studio Code 中启用 IntelliSense for C++?
- python-3.x - Qiskit NameError:未定义名称“q0”
- asp.net-core - React 项目构建错误
- xml - 布局的最底部是什么?如何更改其颜色?