英文:
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 ) => 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论