In a MAUI application, where is the best way to initialize some data when the application starts?

huangapple go评论69阅读模式
英文:

In a MAUI application, where is the best way to initialize some data when the application starts?

问题

在我的MAUI应用程序中,我想要在应用程序启动时初始化一些应用程序的数据。

在我的情况下,我有一个默认设置的应用程序,它在编译时作为资源添加。在启动时,我会检查是否在Preferences.Default中有任何这些设置,这是应用程序的存储位置。如果没有,我会将设置从嵌入的资源复制到References.Default。

目前,我是在MauiProgram.cs文件中执行此操作,但我不确定这是否是最佳位置。

也许AppShell.xaml.cs是另一个可能的位置,或者也可能是app.xaml.cs。

我应该在哪里进行此初始化操作?

AppShell、App和MauiProgram的用途是什么?

谢谢。

英文:

I have a MAUI application and I would like to initialize some data of the application when the application starts.

In my case, I have a default settings applications that is added as resource when I compile. In the startup, I check if I have any of this settings in Prefereces.Default, that is the storage of the application. If not, I copy the settings from the embedded resource to References.Default.

By the moment I am doing this in the MauiProgram.cs file, but I am not sure if this is the best place to do it.

Perhaps AppShell.xaml.cs is another possible place or perhaps app.xaml.cs.

Where should do this initialization?

where is the use of AppShell, App and MauiProgram?

Thanks.

答案1

得分: 1

Declare a page for initialization and use that as the App.MainPage. When the initialization is done, change the App.MainPage to an AppShell instance.

public partial class App : Application
{
    public App( InitPage init, AppShell shell )
    {
        InitializeComponent();
        // InitPage notifies on completion, so we change the MainPage to shell
        init.InitCompleted += ( s, e ) => MainPage = shell;
        MainPage = init;
    }
}

InitPage starts the initialization on appearing. The initialization itself is done inside the ViewModel.

public partial class InitPage : ContentPage
{
    public InitPage( InitPageViewModel viewModel )
    {
        InitializeComponent();
        BindingContext = viewModel;
        Appearing += async ( s, e ) =>
        {
            await viewModel.InitializeAsync();
            InitCompleted?.Invoke( this, EventArgs.Empty );
        };
    }

    public event EventHandler<EventArgs> InitCompleted;
}

The ViewModel now can initialize any needed services.

public class InitPageViewModel : ViewModelBase
{
    [Reactive] public string InitSection { get; private set; }
    protected override async Task OnInitializeAsync( CancellationToken cancellationToken )
    {
        await base.OnInitializeAsync( cancellationToken );
        InitSection = "Warm up DataServices";
        await Task.Delay( 2000 );
        InitSection = "Expand Images";
        await Task.Delay( 4000 );
        InitSection = "Feed the pets";
        await Task.Delay( 3000 );
    }
}

A full sample app is on GitHub.

英文:

Declare a page for initialization and use that as the App.MainPage. When the initialization is done change the App.MainPage to an AppShell instance.

public partial class App : Application
{
    public App( InitPage init, AppShell shell )
    {
        InitializeComponent();
        // InitPage notifies on completion, so we change the MainPage to shell
        init.InitCompleted += ( s, e ) =&gt; MainPage = shell;
        MainPage = init;
    }
}

InitPage starts the initialization on appearing. The initialization itself is done inside the ViewModel

public partial class InitPage : ContentPage
{
    public InitPage( InitPageViewModel viewModel )
    {
        InitializeComponent();
        BindingContext = viewModel;
        Appearing += async ( s, e ) =&gt;
        {
            await viewModel.InitializeAsync();
            InitCompleted?.Invoke( this, EventArgs.Empty );
        };
    }

    public event EventHandler&lt;EventArgs&gt; InitCompleted;
}

The ViewModel now can initialize any needed services

public class InitPageViewModel : ViewModelBase
{
    [Reactive] public string InitSection { get; private set; }
    protected override async Task OnInitializeAsync( CancellationToken cancellationToken )
    {
        await base.OnInitializeAsync( cancellationToken );
        InitSection = &quot;Warm up DataServices&quot;;
        await Task.Delay( 2000 );
        InitSection = &quot;Expand Images&quot;;
        await Task.Delay( 4000 );
        InitSection = &quot;Feed the pets&quot;;
        await Task.Delay( 3000 );
    }
}

A full sample app is on github

huangapple
  • 本文由 发表于 2023年5月14日 19:23:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定