Skip to content

Startup class

Nikita edited this page Jan 29, 2022 · 6 revisions

Introduction

The Startup class is where:

  • Services required by the app are configured.
  • The app's pages building pipeline is defined, as a series of middleware components.
  • The app's elements building pipeline is defined, as a series of middleware components.
  • The app's navigable pages added into the routing.

Here's a sample Startup class:

public class Startup : IAppStartup
{
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddVisualProcessingCore();

            services
                .AddSingleton<IApplicationService, ApplicationService>()
                .AddSingleton<IAnalyticsService, AnalyticsService>()
                .AddSingleton<ILanguageService, LanguageService>()
                .AddSingleton<IMessageService, MessageService>()
                .AddSingleton<ISettingsService, SettingsService>()

                .AddSingleton<WelcomeViewModel>()

                .AddSingleton<App>()
                .AddSingleton<AppTabbedPage>();
        }

        public void ConfigurePage(IPageBuilder pageProcessing)
        {
            pageProcessing
                .AssignPageAppearing()
                .AssignPageDisappearing();

            pageProcessing.ProcessPageElements();
        }

        public void ConfigureElement(IElementBuilder elementProcessing)
        {
            elementProcessing
                .AssignBindingContext()
                .AssignChildrenBindingContext();

            elementProcessing
                .AssignAttachedAsyncCommands()
                .AssignAttachedCommands()
                .AssignAsyncCommands()
                .AssignCommands();
        }

        public void RegisterRoutes()
        {
            Routing.RegisterRoute("welcomePage", typeof(WelcomePage));
            Routing.RegisterRoute("otherPage", typeof(OtherPage));
        }
}

This Startup class has a slight difference with the one, that is packed with ASP.NET Core template: you must derive from IAppStartup type. This is considered as a mandatory thing for this library, because of performance aspect. Classic, "true" ASP.NET Core variant hugely consumes reflection, making this class modular and dynamic, but also introducing unnecessary and heavy additional steps to form the instance of Startup class here in mobile architecture. Although, i should mention, that you're also able to derive from IStartup interface in ASP.NET Core.

Dependency injection (services)

Just like ASP.NET Core, you can register your services using ConfigureServices method.

Sample code:

public void ConfigureServices(IServiceCollection services)
{
	services.AddVisualProcessingCore();
        services.AddFirstServices()
            .AddSecondServices()
            .AddThirdServices();
}

Note, that AddFirstServices and below methods were formed as extension methods for IServiceCollection type. It's a good way to keep your project's Startup class clean and beautiful :)

Page building

This library includes built-in mechanism providing fluent way to build your in-app pages. You can get some pre-configured middleware extensions from AppHosting.Xamarin.Forms library.

Example:

public void ConfigurePage(IPageBuilder pageProcessing)
{
	pageProcessing
            .AssignPageAppearing()
            .AssignPageDisappearing();

        pageProcessing.ProcessPageElements();
}

Basically, what happens with the code above is you're telling to process each page for page appearing attribute (if any), then page disappearing attributes (if any), then locate and setup any element, for which a particular ProcessElement attribute was added.

Note that the order of calling this processing matters!

Read more here.

Element building

This library also helps you determine how you want to setup elements on your page. Again, you can get some pre-configured middleware extensions from AppHosting.Xamarin.Forms library.

Example:

public void ConfigureElement(IElementBuilder elementProcessing)
{
	elementProcessing
            .AssignBindingContext()
            .AssignChildrenBindingContext();

        elementProcessing
            .AssignAttachedAsyncCommands()
            .AssignAttachedCommands()
            .AssignAsyncCommands()
            .AssignCommands();
}

Basically, what happens with the code above is you're telling to setup each element with binding context, then try to assign binding contexts to any elements that may have been specified, then connect commands for a particular element you've defined.

Note that the order of calling this processing matters!

Also note, that a page is considered as an element too and element processing will occur before page processing.

Read more here.

Routes registration

Just a convenient way for pages registration.

Example:

public void RegisterRoutes()
{
	Routing.RegisterRoute("welcomePage", typeof(WelcomePage));
        Routing.RegisterRoute("otherPage", typeof(OtherPage));
}