DependencyServiceExtension - markjulmar/xamu-infrastructure GitHub Wiki
DependencyServiceExtension
The DependencyServiceExtension is a markup extension that provides access to the Xamarin.Forms DependencyService.Get static method in XAML.
Properties
FetchTarget: theDependencyFetchTarget(GlobalInstance or NewInstance) to retrieve. Defaults toGlobalInstance.Type: theSystem.Typeto 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}}">