I am using partial classes to implement platform specific behavior in a .NET MAUI app:
Stem:
public partial class MyServices
{
public partial void DoSomething();
}
Android/iOS/MacCatalyst/Windows/Tizen specific implementations all look similar to this:
public partial class MyServices
{
public partial void DoSomething()
{
// Android/iOS/MacCatalyst/Windows/Tizen specific implementation
}
}
So far, so normal for MAUI (although the platform specific implementation could be done differently, but the partial class approach is common for MAUI and seemed convenient).
Now, in order to be able to execute unit tests (xUnit), it is necessary to add the net7.0
target to the <TargetFrameworks>
in the .csproj file of the SingleProject like this:
<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>
This is just like Gerald Versluis describes in his YouTube video. The relevant code sample can be found here: https://github.com/jfversluis/MauixUnitTestSample/blob/main/MauixUnitTestSample/MauixUnitTestSample.csproj#L5
And this is where my problems start:
Due to the net7.0
target and a missing implementation of the MyServices
class, I now receive this compiler error:
CS8795 Partial method 'MyServices.DoSomething()' must have an implementation part because it has accessibility modifiers. MySampleApp (net7.0)
I haven't found any way to add a (dummy) implementation for the partial MyServices
class to target net7.0
yet. However, I cannot remove the net7.0
target, because then I cannot run the unit tests anymore.