Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Xamarin Blueprints

You're reading from   Xamarin Blueprints Leverage the power of Xamarin to create stunning cross-platform and native apps

Arrow left icon
Product type Paperback
Published in Sep 2016
Publisher Packt
ISBN-13 9781785887444
Length 516 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Michael Williams Michael Williams
Author Profile Icon Michael Williams
Michael Williams
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Building a Gallery Application FREE CHAPTER 2. Building a SpeechTalk Application 3. Building a GPS Locator Application 4. Building an Audio Player Application 5. Building a Stocklist Application 6. Building a Chat Application 7. Building a File Storage Application 8. Building a Camera Application

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 
    {
        #region Public Events
          public event PropertyChangedEventHandler PropertyChanged;
          public event EventHandler<string> Alert;
        #endregion
        #region Private Properties
          private...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image