How to use dependency service in xamarin native (Xamarin.iOS and Xamarin.Android)

XamDev03XamDev03 USMember ✭✭
edited July 8 in General

Hello Folks,

I need suggestion on one point. Let me explain the problem in detail.
I am working on one of my Xamarin native project for Android and iOS. Here, I have two different projects for Android and iOS.

For instance, Android Project Contains total 5 projects:

  • DemoAndroid
  • DemoAndroid.Common
  • DemoAndroid.SQLite
  • DemoAndroid.Services
  • DemoAndroid.Models

iOS Project Contains total 5 projects:
* DemoIOS
* DemoIOS.Common
* DemoIOS.SQLite
* DemoIOS.Services
* DemoIOS.Models

These both projects(iOS and Android) have different core projects instead of common as we are using Xamarin platform. Both core version projects are a mostly similar version of the code. no changes in the code.

Currently, I am working to make a single core based project for Android and iOS.
I am facing the issue like, DemoAndroid.Services and DemoIOS.Services have some platform-specific code of Android and iOS respectively (i.e, date converter, activity indicator etc). I had removed platform specific code and make sure that I will use C# based code in PCL. Now, I need a help here is that, How can I use platform specific code in PCL multiplatform project. Do we have anyway?

NOTE:
I have checked the dependency services but this is working with Xamarin forms and I have Xamarin native code. Do we have any way to stabilize my issue? In addition, I am planning to convert the whole UI version in the Xamarin forms but I want to use same PCL core projects inside the Forms Projects.

So,
1. Do we have any way to stabilize this issue in xamarin native?
2. Are there any possibilities to use same PCL core projects in Xamarin Forms projects? (AFAIK, this is possible but I want to make sure from experts here. That will boost my confidence to move forward to doing this process.)
3. Suggest me if I am doing anything wrong here. Also, suggest me the best way to full-filling all these requirements.

Thanks in advance..!!!

Answers

  • XamDev03XamDev03 USMember ✭✭

    Anyone?

  • andnesandnes USMember ✭✭

    I take it you do not want to use DI or IoC, but rather use as much cross-platform functionality as possible. Is that correct? You can use the plug-in Xamarin.Essentials if you want to use platform-specific functionality like geolocation, device info etc. in your PCL. Combine this with Xamarin.Forms Embedding and you'll be able to use Xamarin Forms views in your Xamarin Native project. We are currently using this in our project and it works really well!

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭
    If you want to use DI or IoC, then you don't have to use the DependencyService. There are other IoC libraries available - for example TinyIoC and Autofac.
Sign In or Register to comment.