Search code examples
xamarinxamarin.formscustom-renderer

Is DependencyService replacement for Custom renderer


I am little confused about DependencyService compared to CustomRenderer.

Can we use DependencyService in place of CustomRenderer. Where can we use DependencyService?


Solution

  • TL/DR: No, they have different use cases, although they both provide methods to access functions of your target system.

    The concept of cutom renderers is directly coupled to user interface elements. Basically for every kind of Xamarin.Forms controls there is a custom renderer, which delegates the rendering of the controls to system UI framework. Usually it maps a Xamarin.Forms control to a native UI element.

    A dependency (resolved via the dependency service) is more of a general concept of platform specific functions (acutally it's not limited to platform specific implementations). You define an interface abstracting the functionality, for which you will provide platform specific implementations. Platform specific classes that implement this interface and are exposed to DependencyService via DependencyAttribute can be resolved at runtime with DependencyService.Get<T>() (where T has to be your interface type). The objects retrieved from Get can be used without knowing which class exactly implements the interface. It's kind of a very basic dependency injection (well, not really, but sort of) and can be used for any functionality that needs a platform specific implementation, not only for UI concerns.