Question Can I start a content page in OnStart if I'm using AppShell in my Maui desktop app?

bobgatto

New member
Joined
Feb 27, 2024
Messages
1
Location
Hawthorne, NJ
Programming Experience
10+
I'm new to .NET MAUI and what I want to do is write a multi-page application for the desktop (not mobile phones). I'm using shell. If this is the best way to do it, I don't know.
The main page is called PassInfo. However, at startup, I would like to check for any database files on the computer. If there are I would like a content page called SelectDb to display them. After the user selects a database, I would like to close SelectDb and then open PassInfo with the selected connection string.

If no database is found I would like a content page called CreateDb to open first so the user can create a new database. After a new database has been created, I would like CreateDb to close and PassInfo to open with the new connection string.

Here is the code from the App and AppShell files:

App.xaml:
    <ShellContent
        Title=""
        ContentTemplate="{DataTemplate pages:PassInfo}"
        Route="PassInfo" />


App.xaml.cs:
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MainPage = new AppShell();
        }

        protected override void OnStart()
        {
            base.OnStart();

            var dbFiles = GetDb();
            if(dbFiles.Count > 0)
            {
                // Place code here to display SelectDb page
            }
            else
            {
                // Place code here to display CreateDb page
            }
        }

        private List<string> GetDb()
        {
           // GetDb returns a list of directories where any found databases are located

            var files = new List<string>();
            var directories = new string[] { };

            foreach(DriveInfo d in DriveInfo.GetDrives().Where(x => x.IsReady == true))
            {
                try
                {
                    files.AddRange(Directory.GetFiles(d.RootDirectory.FullName, "LakDb*.db", SearchOption.TopDirectoryOnly));
                    directories = Directory.GetDirectories(d.RootDirectory.FullName);
                }
                catch(UnauthorizedAccessException)
                {
                    // Do Nothing And Continue.
                }

                foreach(var DirVal in directories)
                {
                    try
                    {
                        files.AddRange(Directory.GetFiles(DirVal, "LakDb*.db"));
                    }
                    catch(UnauthorizedAccessException)
                    {
                        // Do Nothing.
                    }
                }
            }

            return files;
        }
    }


AppShell.xaml:
    <ShellContent
        Title=""
        ContentTemplate="{DataTemplate pages:PassInfo}"
        Route="PassInfo" />

And my AppShell.xaml.cs file has:

AppShell.xaml.cs:
        public AppShell()
        {
            InitializeComponent();
            Routing.RegisterRoute("selectdb", typeof(SelectDb));
            Routing.RegisterRoute("createdb", typeof(CreateDb));
        }

So my confusion starts at the OnStart method. I registered two pages in the AppShell which I believe won't come into play until after the OnStart method in the App file which I believe gets executed first.
So will this cause a problem? if so, how can I fix it?
 
Back
Top Bottom