Book Image

Cross-platform UI Development with Xamarin.Forms

By : Paul Johnson
Book Image

Cross-platform UI Development with Xamarin.Forms

By: Paul Johnson

Overview of this book

Table of Contents (22 chapters)
Cross-platform UI Development with Xamarin.Forms
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
In the Beginning…
Index

Setting up the event system and interface


The interface only needs to be something as simple as the following code:

namespace connectivity
{
  public interface IConnectivity
  {
    bool NetworkConnected();
  }
}

Note

The source for the connectivity part of the chapter can be found in Chapter13/Connectivity.

To track the state of the connection, a bool variable is set up in App:

public static App Self {get; private set;}

public bool IsConnected {get; private set;}

public App()
{
  App.Self = this;
  IsConnected = DependencyService.Get<IConnectivity>().NetworkConnected();

The reason why IsConnected is set to private set; is because the listener for the event change is in App. We will keep all the connectivity parts in one place.

The handler for the event is also fairly simple, although we need to also broadcast back to the platform to send an internal notification, as shown in the following code:

public UIChangedEvent MessageEvent { get; set;}
MessageEvent = new UIChangedEvent();

MessageEvent...