Handling alerts in view-models

Handling alerts via view-models is important as we handle many errors via try/catch statements. To respond to these errors, we want to display an alert dialog showing the error message to the user. There are two ways we are going to do this:

  • Using an EventHandler for pushing events to the current page so that we can call the DisplayAlert function with different messages
  • Using an interface for dependency injection where we will implement native alerts

Our first step is to add the ViewModelBase class; this is where alerts will be fired from.

Create a new folder in the FileStorage.Portable project called ViewModels, add a new file called ViewModelBase.cs, and implement the following:

public class ViewModelBase : INotifyPropertyChanged ...

Get Xamarin Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.