Reputation: 1571
I am currently moving an mvvmlight
app to use Microsoft.Toolkit.Mvvm
and for the most part, it has been relatively pain free.
I am having an issue though with the addition of services to Ioc.Default.ConfigureServices.
On the platform specfic code, I create a service collection to handle platform specific things (such as Storage, UserSettings etc) like this
Ioc.Default.ConfigureServices(new ServiceCollection().
AddSingleton<ISqliteConnectionFactory, SQLConnection>().
AddSingleton<IFileservice, Storage>().
AddSingleton<IUserSettings, UserSettings>().BuildServiceProvider());
This works fine on both iOS and Android. However, in my ViewModelLocator
(from the old mvvmlight package), I cannot add anything to the services as the default service collection has already been set up.
Is there a way to add the services in the shared code to the Ioc.Default.ConfigureServices
once it has been created?
Upvotes: 1
Views: 510
Reputation: 1571
I found a simple solution.
Create the servicecollection on the platform and pass to the App method as an IServiceCollection. This is then passed in the App to the ViewModelLocatior and the services added from there before calling the BuildServiceProvider
method.
Upvotes: 0