Skip to content
This repository has been archived by the owner on Aug 27, 2020. It is now read-only.
Mark Smith edited this page Sep 2, 2016 · 17 revisions

Initializing the library

You can call the static XamUInfrastructure.Init method to do the common initialization tasks. This should only be called once. It registers each of the built-in implementations with the defined abstractions:

  • FormsNavigationPageService => INavigationService
  • FormsMessageVisualizerService => IMessageVisualizerService
  • DependencyServiceWrapper => IDependencyService
// Initialize our services
IDependencyService ds = XamarinUniversity.Services.XamUInfrastructure.Init();
ds.Register<MainViewModel>();
ds.Register<ISomeOtherService, SomeImplementation>();
...

Changing the Service Locator

By default, the library uses the static DependencyService built into Xamarin.Forms. However, you can change this by supplying a different implementation of the IDependencyService abstraction to the XamUInfrastructure.Init() method. For example, you could wrap the Unity Container.

First, turn it into a IDependencyService:

public class UnityWrapper : IDependencyService
{
    UnityContainer container;

    public UnityWrapper(UnityContainer container)
    {
       this.container = container;
    }

    public T Get<T>() where T : class
    {
        return container.Resolve<T>();
    }

    public void Register<T> () where T : class, new()
    {
        container.RegisterType<T>();
    }

    public void Register<T, TImpl> () 
        where T : class
        where TImpl : class, T, new()
    {
        container.RegisterType<T,TImpl>();
    }
}

Then, pass your implementation into the Init method:

// Setup Unity
var container = new UnityWrapper(new UnityContainer());

// Initialize our services - pass in the container we want to use
IDependencyService ds = XamarinUniversity.Services.XamUInfrastructure.Init(container);
ds.Register<MainViewModel>();
ds.Register<ISomeOtherService, SomeImplementation>();
...

You can then retrieve this from anywhere in your code through the static XamUInfrastructure.ServiceLocator property.

Available Types

This is a list of the reusable classes included in the library. Most are independent and have not required dependencies on each other so you can pick and choose what you utilize in your code. For example you can choose to use the message visualizer service but not the navigation service.

XamUInfrastructure - static class to initialize and retrieve the service locator.

Commands

Data Binding

Behaviors

Controls

  • ItemsControl - a simple bindable template creator which will instantiate a set of Label elements or DataTemplate driven elements from a bound list. Similar to a ListView, but the produced content is not scrollable, nor does it provide any interactivity outside the generate content.

Value Converters

Collections

Extension methods

Interfaces

  • IAsyncDelegateCommand - an interface to expose ExecuteAsync. Derives from IDelegateCommand.
  • IDelegateCommand - an interface to expose RaiseCanExecuteChanged. This derives from ICommand.
  • IDependencyService - an abstraction over a typical Service Locator such as Xamarin.Forms DependencyService.
  • IMessageVisualizerService - an abstraction over a "MessageBox" display such as Xamarin.Forms Page.DisplayAlert.
  • INavigationService - an abstraction for a basic navigation service using string-based keys to represent the pages you can navigate to.

Markup Extensions

MVVM classes

  • AsyncDelegateCommand - an implementation of IAsyncDelegateCommand.
  • DelegateCommand - an implementation of IDelegateCommand. Useful to avoid dependencies in your ViewModels on Xamarin.Forms.
  • PropertyObserver - a fluid property observer which lets you easily monitor PropertyChange notifications on child ViewModels.
  • SimpleViewModel - a base ViewModel implementation for MVVM which supports property change notifications.

Services

  • DependencyServiceWrapper - a wrapper around Xamarin.Forms static DependencyService class to turn it into a service based on IDependencyService.
  • FormsMessageVisualizerService - a wrapper around Xamarin.Forms Page.DisplayAlert method to turn it into a service based on IMessageVisualizerService.
  • FormsNavigationPageService - an example service which implements the included INavigationService and supports basic stack-based navigation through Xamarin.Forms.