Skip to content
This repository was archived by the owner on Aug 27, 2020. It is now read-only.

DependencyServiceExtension

Mark Smith edited this page Aug 29, 2016 · 1 revision

DependencyServiceExtension

The DependencyServiceExtension is a markup extension that provides access to the Xamarin.Forms DependencyService.Get static method in XAML.

Properties

  • FetchTarget : the DependencyFetchTarget (GlobalInstance or NewInstance) to retrieve. Defaults to GlobalInstance.
  • Type : the System.Type to retrieve. This is required and is the default content for the extension when used in XAML.

Example

We could register a ViewModel in our App constructor:

public class App
{
    public App()
    {
       DependencyService.Register<MainViewModel,MainViewModel>();
    }
    ...
}

and then find that dependency in XAML and set it to our binding context:

<Page xmlns:inf="clr-namespace:XamarinUniversity.Infrastructure;assembly=XamU.Infrastructure" ...
     BindingContext="{inf:DependencyService {x:Type vm:MainViewModel}}">