Xamarin & MvvmCross Handbook
  • Introduction
  • Using MvvmCross to create your first Xamarin App
    • Creating the Core project
      • Understanding MVVM
      • Adding a simple Service
      • Adding a ViewModel
      • Adding an App class
    • Creating the Android project
      • Adding an Android Application class
      • Adding the Android Layout View (AXML)
      • Understanding the data-binding syntax
      • Adding the View class
    • Creating the iOS project
      • Updating the AppDelegate class
      • Adding the iOS View for the first ViewModel
      • Understanding the data-binding syntax
    • Creating the UWP project
      • Updating the App.xaml.cs and the App.xaml
      • Adding the UWP View
      • Understanding the data-binding syntax
    • Creating the REST API Service
      • Creating the models
      • Creating the interface and the implementation for the PostService
      • Creating the ViewModel
      • Adding the View for the Android project
      • Adding the View for the iOS project
      • Adding the View for the UWP project
    • Navigation
      • Creating the PostDetail View & ViewModel
      • Command with the parameter
      • Retrieving the param from the previous ViewModel
      • Understanding the IMvxNavigationService
      • Responding the events from different controls in the ListView
      • Retrieving the return result from the previous ViewModel
    • Creating the Xamarin.Forms project
      • Creating the Forms.UI project
      • Adding the App.xaml and the App.xaml.cs
      • Adding the View
      • Creating the Android project
      • Creating the iOS project
      • Creating the UWP project
      • Summary For Forms
    • Summary
  • Implementing MasterDetail layout in Xamarin.Forms by MvvmCross
    • Introduction
    • Creating the project by MvxScaffolding
    • Creating the MasterDetailPage
      • Creating the ViewModel
      • Creating the XAML file
    • Creating the MasterPage
      • Creating the ViewModel
      • Creating the XAML file
    • Creating the DetailPages
      • Creating the ViewModels
      • Creating the XAML files
    • Implementing the Menu functionalities
      • Displaying the MasterPage and the DetailPage
      • Setting the menu navigation
      • The other approaches to set the data-binding
    • Fine-tuning the UI
      • Adding the hamburger icon for iOS
      • Adding the header bar for Android and iOS
      • Adjust the height of the item for UWP
    • Summary
Powered by GitBook
On this page

Was this helpful?

  1. Using MvvmCross to create your first Xamarin App
  2. Creating the Core project

Adding an App class

MvvmCross use an App class to take responsibility for registering custom object on the IoC container and start the ViewModels. Add an App class to the root folder and update the content like this:

using MvvmCross.IoC;
using MvvmCross.ViewModels;
using MvvmCrossDemo.Core.Models;
using MvvmCrossDemo.Core.ViewModels;

namespace MvvmCrossDemo.Core
{
    public class App : MvxApplication
    {
        public override void Initialize()
        {
            CreatableTypes()
                .EndingWith("Service")
                .AsInterfaces()
                .RegisterAsLazySingleton();
            RegisterAppStart<FirstViewModel>();
            ModelMapper.Init();
        }

    }
}

The App class inherits from MvxApplication class which comes from MvvmCross. You can name it as you like but I recommend you call it App. In this class we do two important things, one is to register all the services as singleton pattern (because usually, the names of these services end with “Service”), another is to call a ViewModel named FirstViewModel to show the first view when the app starts, which I described in the previous section.

The code uses Reflection to find all classes in the MvvmCrossDemo.Core assembly which have a public constructor and with names ending in Service, then find their interfaces and register them as lazy singletons. Lazy means it will not be created until it is first to be used in the app lifecycle. Using Service as the name ending is a convention. You can change it, but it is highly recommended to follow the convention.

MvvmCross provides us with an IoC container to manage the implementation for all the services. With the registration, the ViewModel will get the instance of the interface automatically without initializing it through the constructor injection, which is a common method to implement Dependency Injection (The other methods contain setter injection, method injection, etc.), like this:

public FirstViewModel(IGreetingService greetingService)
{
      _greetingService = greetingService;
}

MvvmCross also provides us with some other explicit manual methods to register the services, as shown below:

Mvx.IoCProvider.RegisterSingleton<IGreetingService>(new GreetingService());
Mvx.IoCProvider.RegisterType<IGreetingService, GreetingService>();

You can choose one way you like. Now whenever you need to use an IGreetingService reference, the IoC container will create an IGreetingService instance automatically. You can also use this code to get an implementation explicitly:

Mvx.IoCProvider.Resolve<IGreetingService>();

There are a lot of similar methods and overrides to register and resolve the interfaces and the implementations for different lifecycles, such as singleton, dynamic, or lazy construction.

To sum up this section, we have completed these steps:

  • We created the Core project for all specific platforms based on .NET Standard.

  • We created a service interface and the implementation of it.

  • We created an App class to register the services and call the first ViewModel/View.

  • We created one ViewModel and inject the service instance into it to complete the logic.

  • We also added the code snippet to simplify the input.

Now we can add the specific platform projects.

PreviousAdding a ViewModelNextCreating the Android project

Last updated 6 years ago

Was this helpful?

If you are not familiar with DI(Dependency Injection) or IoC(Inversion of Control), you can find more details here: . I strongly recommend that you know what Dependency Injection is, which is an essential pattern to build an application with the clean architecture.

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1