Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

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

$
0
0

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.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>