Xamarin.Mac - Dependency Injection / IoC examples?

Hey folks,

I've found lots of good examples on Dependency Injection (DI) and Inversion of Control (IoC) for Xamarin.Forums, however I haven't been able to find anything similar for Xamarin.Mac.

There's a Dependency Service in the Xamarin.Forms name space ( Xamarin.Forms.Dependency), but nothing for Xamarin.Mac; do I have to use Xamarin Forms + Mac for this magic?

Reference Material: https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/dependency-service/introduction/

I think I'm just being dumb, any help to point me in the right direction would be great!


  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University, Xamarin Moderator Xamurai

    Generally you would only need DependencyInjection into a project that would have multiple ultimate target platforms, IOW into a project that will be referenced by multiple platform specific projects. Since Xamarin.Mac is already a Platform specific project, it makes no sense to have a built in dependency injection system in Xamarin.Mac, unlike Xamarin.Forms which is meant to be references by Xam.Android, Xam.iOS, UWP, and now Xam.Mac platform specific app projects. So Forms DependencyService allows you to inject platform specific implementations into the Xamarin.Forms core PCL project.

    So the first question that comes up for me is, what is it you are trying to do that you think you need Dependency Injection for a Xamarin.Mac project as a Xamarin.Mac project only targets one specific platform?

    You can certainly add a Xamarin.Mac project to your Xamarin.Forms solution and use the Xam.Forms DependencyService to inject implementations from Xam.mac project to the Xamarin.Forms PCL project.

  • urashidurashid USMember

    Thank you for the quick reply!

    We're in the process of building a Windows and Mac desktop application (it's fairly basic), there wont be mobile implementation of this app.

    I was curious if there is a solution where I wouldn't have to use Xamarin.Forms and could use Container to inject classes in my View Models. Based on your answer it sounds like if I want to share UI code (Models, View Models) we should use something like Xamarin Forms. Does that make sense ?

    Again thank you for your help, pardon my ignorance I'm coming from the ASP.NET world :smile: Xamarin and MacOS development has been a bit of a learning experience.

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University, Xamarin Moderator Xamurai

    You do not need to use Xam.Forms. You could just use a .NET Standard library for your shared code as long as both of your platforms conform to the .NET standard 2.0 specification. Xam.Mac Mobile does. More information on Xamarin and .NET Standard: https://blog.xamarin.com/share-code-net-standard-2-0/

    However you will not have the Xamarin.Forms DependencyService. Instead you can use some 3rd party IoC (Inversion of Control) library that would ease doing dependency injection/IoC.

    Xamarin University has a great class on doing all of this called "Advanced Cross-Platform Mobile Development" and goes over three methods to inject platform specific code/types so they can be used in your shared library.

    We do have a guide that may help. Even though this is a Forms guide, the concepts discussed can apply even to a non-forms solution... you just won't get the Forms DependencyService. See: https://developer.xamarin.com/guides/xamarin-forms/enterprise-application-patterns/dependency-injection/

Sign In or Register to comment.