Skip to end of metadata
Go to start of metadata

Crosslight is designed with MVVM pattern which allows clear separation of models, views and user interaction logic. As the result, a Crosslight app typically comprises of several layers and components which must be registered and configured at the startup of the application. Crosslight provides a solid architectural design called application service which controls the application's main entry point and maintains its life cycle. You can easily implement an application service by deriving from the ApplicationServiceBase class.

This page discusses the usage of the class in details and its vital role in Crosslight applications, starting from the life cycle overview, setting the root view model, and more.

On this page:


At the heart of every Crosslight application, residing in the core project, is the ApplicationServiceBase class. This class serves as the main entry point of a Crosslight app and contains many important information that controls the lifecycle of the app.

The following table describes the important lifecycle that are executed in Crosslight applications:

OnActivate()Called when the application is activating. Typically, setting the root view model and registering the app for push notification is done here.
OnDeviceTokenReceived(DeviceToken)Called when the app receives a DeviceToken as the result of successful push service registration. Typically, you would also want to register the received device token to the server.
OnNotificationReceived(Notification)Called when a Notification is received. This is the entry point where to handle notification messages.
OnPause()Called when the application is about to pause. The app goes to the background state.
OnRestart()Called when the application is restarting.
OnResume()Called when the application is resuming from the previous state.
OnStart(StartParameter)Called when the application is starting.
OnStop()Called when the application is about to stop.
OnTerminate()Called when the application is terminating.

When the app is starting for the first time, the following lifecycle processes are executed:

  1. OnActivate. Current application state is NotRunning.
  2. OnStart. Current application state is Starting.
  3. OnResume. Current application state is Started.

When the app is terminate due to back navigation, the following lifecycle processes are executed:

  1. OnPause. Current application state is Paused.
  2. OnTerminate. Current application state is Terminating.

When the app is running, and home button is pressed, the following lifecycle processes are executed:

  1. OnPaused. Current application state is Background.

Interface Registration and Application Settings Configuration

Crosslight apps are built with dependency delegation design pattern for maximum separation of concerns. Most of the app's main settings such as AppSettings are configured in a single entry point, that is, in the constructor of the ApplicationServiceBase class. In addition, you can also register additional services and containers in the same place. This design allows better maintainability and greater extensibility in which you can replace a service implementation quickly and easily.

The following code example shows a typical implementation of the Crosslight application service.


As seen from the above code snippet, initialization such as registering the containers, repositories, services, and managing application settings to be used with the WebApi server is all done from a single entry point which is in the constructor of the class.

Setting Root View Model

The ApplicationServiceBase class in Crosslight Core project (typically called App Service) contains many useful information that control the behaviors of Crosslight apps. One of the most important methods is the SetRootViewModel method. By passing the root view model type, Crosslight application will set the view model as the root view model to be displayed.

To learn more about which models to use suitable for each view type, see Selecting a ViewModel Base Class to Get Started.

Changing the RootViewModel Programmatically

In some scenarios, you might want to specify the view model based on certain conditions. For example, when the user is not logged in to your application, you might want to display a LoginViewModel, however, once the user is logged in, you want to redirect user to the HomeViewModel. To do so, see the following code snippet.

In the above code, the account service is invoked to see if the user has logged in to our application. If so, the app will redirect the user to the navigation drawer, otherwise, it will redirect them to the login page.

Crosslight also supports advanced navigation features such as root view navigation and conditional navigation based on target device kind. To learn more about advanced navigation features, see Advanced Navigation Topics.