'Correctly registering services in Xamarin.Forms app with Prism
I am building a Xamarin.Forms application and have decided on using the Prism MVVM framework with Unity as the IoC. It is my first time using Prism, I have been using MVVMLight for my previous app. I liked how Prism looked and felt so I just went with it.
As I was building the application I kept adding services be it platform specific or regular services. Every service I would register inside the RegisterTypes method override inside the App.xaml.cs.
As I did when using MVVMLight, all of my services got registered like containerRegistery.Register<IService, Service>(). But after adding base view models (with common services being injected), and building on the app, everything started to feel very slow when using the app, every tap to navigate took 2-3 seconds to load without seeming to do anything. I noticed that it happened only when navigating.
I built a demo base view model with a couple of pages to see if it was the framework's problem, or something else, but after seeing the instant navigation I started to dig a little further. Turns out it was the service injection from the base view model.
The thing is, when I changed all of my service registration to containerRegistry.RegisterSingleton<IService, Service>() everything is back to buttery smooth.
My question is, why is that? I know that using only Register builds a new instance every time, but I have been successfully using it like that in MVVMLight. And is it a good way to register the services as RegisterSingleton?
Thank you very much!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
