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

FormsMessageVisualizerService

Mark Smith edited this page Sep 1, 2016 · 2 revisions

FormsMessageVisualizerService

The FormsMessageVisualizerService provides a simple wrapper around the Xamarin.Forms Page.DisplayAlert functionality to make it conform to an abstraction - IMessageVisualizerService. This breaks the dependency in a View Model and allows it to be tested properly.

Usage

You can either call the static XamUInfrastructure.Init method to register the service, or register it yourself through your own container. Here's an example of manually registering the service with the built-in DependencyService:

public class App
{
   public App()
   {
      XamarinUniversity.Services.XamUInfrastructure.Init();
      ...
   }
}

or:

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

Then you can retrieve it in your View Model:

public class MyViewModel
{
   IDependencyService ds;
   ...
   void OnError() {
      ds.Get<IMessageVisualizerService>()
          .Show("Error", "Houston, we have a problem", "OK);
   }
}