AvaloniaCommunity / Prism.Avalonia

Prism Avalonia UI framework. Super charge your cross-platform apps with IoC, navigation, and more! Sponsored by Suess Labs. Prism is based on Microsoft patterns and practices.
https://www.nuget.org/packages/Prism.Avalonia/
MIT License
293 stars 38 forks source link

On Async app initializations #123

Closed XGBXGB closed 5 months ago

XGBXGB commented 5 months ago

Hi team,

First time trying avalonia, coming from WPF.

How do you go about registering services that require an async call? i.e. retrieving values from cloud/APIs. I was trying to do all these in app.axaml.cs, by overriding OnInitialize() and RegisterTypes().

Doing this at the very start as I saw that we register and setup services and viewmodels here.

Sample #1:

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    var config = await RetrieveConfig(); 
    //RetrieveConfig().GetAwaiter().GetResult();

    containerRegistry.RegisterSingleton<IDemoService>(() => new DemoService(config.Prop1, config.Prop2));
}

Sample #2:

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    containerRegistry.RegisterSingleton<IConfigService, ConfigService>();
    containerRegistry.RegisterSingleton<IDemoService, DemoService>();
}

public class DemoService : IDemoService
{
    public DemoService(IConfigService configService)
    {
        var config = configService.RetrieveConfig().GetAwaiter().GetResult();

        // set props with retrieved config
    }
}

From these two samples, i've reached variations that:

Any insights, recommendations will be greatly appreciated!

Thanks!

DamianSuess commented 5 months ago

Not an issue. Please open questions such as this in the Discussions forum