Skip to content

Shared application class

Nikita edited this page Jan 30, 2022 · 11 revisions

Introduction

Shared application class App.xaml/App.xaml.cs is available from your .NET Standard project. This page shows new possibilities when using this library.

Dependency injection

Because the entry point of shared application is processing through the host, you're able to inject any necessary dependency into your shared app class. Check this out:

public partial class App : Application
{
    //...

    public App(IApplicationService application,
               IAppHostLifetime appHostLifetime)
    {

        _application = application;
        _appHostLifetime = appHostLifetime;
    }

    //...
}

You can read more about dependency injection here.

App host lifetime notifications

Notifying application host about state changes is necessary for proper hosted services execution pause/resume mechanisms. You can set it up as following:

protected override void OnResume()
{
    _appHostLifetime.NotifyResuming();
}

protected override void OnSleep()
{
    _appHostLifetime.NotifySleeping();
}

Initialization

It is recommended, that you extract the InitializeComponent method call from the constructor into the OnStart method, if possible. You should, of course, put it before any navigation method execution.

protected override void OnStart()
{
    InitializeComponent();
    //...
}

This is useful, especially when you have a huge App.xaml file (if it is the case, consider also splitting it into several smaller .xaml files and merging them using <ResourceDictionary.MergedDictionaries> tag).