我正在使用部分类在 .NET MAUI 应用程序中实现特定于平台的行为:
Stem:
public partial class MyServices
{
public partial void DoSomething();
}
Android/iOS/MacCatalyst/Windows/Tizen 特定实现都与此类似:
public partial class MyServices
{
public partial void DoSomething()
{
// Android/iOS/MacCatalyst/Windows/Tizen specific implementation
}
}
到目前为止,对于 MAUI 来说很正常(尽管特定于平台的实现可以以不同的方式完成,但是部分类方法对于 MAUI 来说很常见并且看起来很方便)。
现在,为了能够执行单元测试(xUnit),有必要添加net7.0
目标为<TargetFrameworks>
in the .csproj文件的单一项目像这样:
<PropertyGroup>
<TargetFrameworks>net7.0;net7.0-android;net7.0-ios;net7.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>
<!-- skipping irrelevant stuff here... -->
<OutputType Condition="'$(TargetFramework)' != 'net7.0'">Exe</OutputType>
<!-- skipping irrelevant stuff here... -->
</PropertyGroup>
这正如杰拉尔德·韦尔斯路易斯 (Gerald Versluis) 在他的著作中所描述的那样Youtube 视频。相关代码示例可以在这里找到:https://github.com/jfversluis/MauixUnitTestSample/blob/main/MauixUnitTestSample/MauixUnitTestSample.csproj#L5
这就是我的问题开始的地方:
因为net7.0
目标和执行的缺失MyServices
类,我现在收到此编译器错误:
CS8795 Partial method 'MyServices.DoSomething()' must have an implementation part because it has accessibility modifiers. MySampleApp (net7.0)
我还没有找到任何方法来为部分添加(虚拟)实现MyServices
目标类别net7.0
然而。但是,我无法删除net7.0
目标,因为那样我就无法再运行单元测试了。