0

Good morning

I am building package for Xamrin.Forms. I follow how its done by James Montenango

Here are some important pieces of my .csproj

<TargetFrameworks>netstandard2.0;MonoAndroid10.0;Xamarin.iOS10</TargetFrameworks>

  <ItemGroup>
    <Compile Include="**\*.shared.cs" />
  </ItemGroup>

  <ItemGroup Condition=" $(TargetFramework.StartsWith('netstandard')) ">
    <PackageReference Include="XTricks.Shared" Version="1.0.1" />
    <PackageReference Include="Xamarin.Essentials" Version="1.6.1" />
  </ItemGroup>

  <ItemGroup Condition=" $(TargetFramework.StartsWith('MonoAndroid')) ">
    <PackageReference Include="Xamarin.Forms" Version="5.0.0.2196" />
    <PackageReference Include="Xamarin.GooglePlayServices.Location" Version="118.0.0.1" />
    <PackageReference Include="XTricks.Shared" Version="1.0.1" />
    <PackageReference Include="Xamarin.Essentials" Version="1.6.1" />
    <Compile Include="**\*.android.cs" />
  </ItemGroup>

  <ItemGroup Condition=" $(TargetFramework.StartsWith('Xamarin.iOS')) ">
    <PackageReference Include="Xamarin.Essentials" Version="1.6.1" />
    <PackageReference Include="XTricks.Shared" Version="1.0.1" />
    <Compile Include="**\*.apple.cs" />
  </ItemGroup>

Everything is working except one important thing. I would like to have two implementation of the same class for Android and for iOS.

For this purpose I declare and interface as:

IMyService.shared.cs

And I create two classes, one in Android folder called: MyService.android.cs which implements IMyService.shared.cs and second in Apple folder called MyService.apple.cs.

Now as is done in above library I would like to use service in shared code.

        private static IMyService Service
        {
            get
            {
#if NETSTANDARD1_0 || NETSTANDARD2_0
                return null;
#else
                return new MyService(); // And this code is red one. MyService cannot be found. 
#endif
            }
        }

Basically I am stucked there. Does anyone know how to move foward?

miechooy
  • 3,178
  • 12
  • 34
  • 59
  • 1
    Does [this article](https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction) help? Dependency Injection is the easiest way to resolve a platform-specific service class. – Andrew H Oct 20 '21 at 16:37
  • @AndrewH thank you, however its not solution for this problem – miechooy Oct 20 '21 at 18:48
  • 1
    It's exactly the solution as you presented the problem: "I would like to have two implementation of the same class for Android and for iOS." If you have a different problem, please update the question. – Andrew H Oct 20 '21 at 18:56

1 Answers1

0

Make sure you have same namespace to MyService class with your shared codes and then you just build your library.

cahyo
  • 597
  • 2
  • 5
  • 14