Xamarin Foms, Entity Framework Core, SQLite, Prism MainPage ViewModel initialised before App()

ledragonledragon Member ✭✭

Hi,

First post here (of many more I expect) so firstly let me say hello.

I'm having a little difficulty figuring the right approach to the following using Prism. I'm using SQLite with EFCore and following the docs have established the DB path in MainActivity as follows:

var dbName = "xxxx.db3";
var folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var dbPath = Path.Combine(folderPath, dbName);

var androidInitialiser = new AndroidInitializer();

LoadApplication(new App(androidInitialiser, dbPath));

So the DB Path is passed into the App constructor. Again as per docs I Navigate to the MainPage in App.xaml.cs OnInitialized():

public static string DbPath;

public App(IPlatformInitializer initializer, string databasePath) : base(initializer)
{
    DbPath = databasePath;
}

protected override async void OnInitialized()
{
    InitializeComponent();

    await NavigationService.NavigateAsync("MainPage", null, false, false);    
}

Which instantiates the MainPageViewModel. I'm injecting a sqlite database service into the MainPageViewModel:

public MainPageViewModel(INavigationService navigationService, IJobService jobService) : base(navigationService)
{
    _jobService = jobService;
}

Which in turn creates the EF DbContext using the database path from the App constructor:

public JobService()
{
    _context = new OPOCDbContext(App.DbPath);
}

The App constructor is called after OnInitialised and consequently also the MainPageViewModel though so App.DpPath is null.

Can anyone offer a solution as to how I can reorder this so the DBPath is not null when the MainPageViewModel is initialised?

Thank in advance for any guidance received.

Best Answer

  • ledragonledragon ✭✭
    edited April 14 Accepted Answer

    Ok I just moved the navigation into the App constructor:

    /// <summary>
    /// 
    /// </summary>
    /// <param name="initializer"></param>
    /// <param name="databasePath"></param>
    public App(IPlatformInitializer initializer, string databasePath) : base(initializer)
    {
         DbPath = databasePath;
         NavigationService.NavigateAsync("MainPage", null, false, false);
    }
    

    And it works...is there any reason I shouldn't do this?

    Thanks

Answers

  • ledragonledragon Member ✭✭
    edited April 14 Accepted Answer

    Ok I just moved the navigation into the App constructor:

    /// <summary>
    /// 
    /// </summary>
    /// <param name="initializer"></param>
    /// <param name="databasePath"></param>
    public App(IPlatformInitializer initializer, string databasePath) : base(initializer)
    {
         DbPath = databasePath;
         NavigationService.NavigateAsync("MainPage", null, false, false);
    }
    

    And it works...is there any reason I shouldn't do this?

    Thanks

  • Tracy320Tracy320 Member ✭✭

    Congrats , and thanks for shaing the answer.

Sign In or Register to comment.