'.Net maui database import need way to reload application / close application

I have recently added an import/export database feature to my app. I am looking for a way to quit and reload my .net maui application after an import has been done. I am having issues just trying to quit the application with Application.Current.Quit. Suggestions?

Also, are there any easy ways to set up a file picker on Maui to prevent hard coding file paths?



Solution 1:[1]

Perhaps you don't actually need to restart the app?

In your cross-platform App.xam.cs App constructor, there is a line something like MainPage = new MainPage();. You can do the equivalent from anywhere:

Application.Current.MainPage = new MainPage();

Also, looking at App lifecycle, you might add code to be performed when app starts up or shuts down. Refactor those so you can call them explicitly:

public partial class App : Application
{
    protected override void OnResumed()
    {
        MyOnResumed();
    }
    public void MyOnResumed()
    {
        ...
    }

    protected override void OnStopped()
    {
        MyOnStopped();
    }
    public void MyOnStopped()
    {
        ...
    }
}

Then you could perform that cleanup and initialization:

    var app = Application.Current as MyNameSpace.App;
    app.MyOnStopped();
    app.MainPage = new MainPage();
    app.MyOnResumed();

The goal is to write your app in such a way that any necessary cleanup and initialization is performed in those two methods.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ToolmakerSteve