MAUI:如何在 SingleProject 中使用部分类来实现平台特定的实现以及 net7.0 作为 TargetFramework?

2023-11-29

我正在使用部分类在 .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目标,因为那样我就无法再运行单元测试了。


感谢 Gerald Versluis,我根据状态栏实现MAUI 社区工具包。所以,这是我的解决方案:

我改变了一种稍微不同的方法。我没有将部分类添加到平台文件夹,而是添加了一个新的文件夹和命名空间,并使用基于文件名的多目标,如下所述:https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/configure-multi-targeting#configure-filename-based-multi-targeting

我的实现文件现在都在同一个文件夹中:

Services/Platform/
    MyServices.shared.cs
    MyServices.android.cs
    MyServices.ios.cs
    MyServices.windows.cs
    MyServices.net.cs

The MyServices.shared.cs文件包含方法声明,而特定于平台的实现则驻留在特定于平台的文件中。

In the .csproj我在里面添加了以下内容<Project> node:

 <!-- Android -->
  <ItemGroup Condition="$(TargetFramework.StartsWith('net7.0-android')) != true">
    <Compile Remove="**\**\*.Android.cs" />
    <None Include="**\**\*.Android.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
  </ItemGroup>

  <!-- iOS -->
  <ItemGroup Condition="$(TargetFramework.StartsWith('net7.0-ios')) != true">
    <Compile Remove="**\**\*.iOS.cs" />
    <None Include="**\**\*.iOS.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
  </ItemGroup>

  <!-- Mac Catalyst -->
  <ItemGroup Condition="$(TargetFramework.StartsWith('net7.0-maccatalyst')) != true">
    <Compile Remove="**\**\*.MacCatalyst.cs" />
    <None Include="**\**\*.MacCatalyst.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
  </ItemGroup>

  <!-- Windows -->
  <ItemGroup Condition="$(TargetFramework.StartsWith('net7.0-windows')) != true">
    <Compile Remove="**\*.Windows.cs" />
    <None Include="**\*.Windows.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
  </ItemGroup>

  <!-- .NET -->
  <ItemGroup Condition="!($(TargetFramework.StartsWith('net')) == true AND $(TargetFramework.EndsWith('.0')) == true AND $(TargetFramework.Contains('-')) != true)">
    <!-- e.g net6.0 or net7.0 -->
    <Compile Remove="**\*.net.cs" />
    <None Include="**\*.net.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
  </ItemGroup>

它就像一个魅力,我也可以运行单元测试。完美的设置。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MAUI:如何在 SingleProject 中使用部分类来实现平台特定的实现以及 net7.0 作为 TargetFramework? 的相关文章

随机推荐