MAUI DependencyService.Get returning null
We are migrating from Xamarin to MAUI our app.
In the old xamarin version we used in a class directly the:
DependencyService.Get<IMessage>(). ShortAlert(“put a valid email”);
where the interface is getting the right Call to the realtive platform main funcions.
now, in both the android and ios class implementation we still have the old [assembly: Dependency(typeof(MessageAndroid))]
and [assembly: Dependency(typeof(MessageIOS))]
We put in the MauiProgram the singleton registration by platform selector as: #if IOS builder. Services.AddSingleton<IMessage, MessageIOS>();
and, in all the ViewModels of the app we can pass into the constructor the instance of the class and all is working good because we can call directly the _message. ShortAlert(“put a valid email”); and all is working perfectly…
We have the problem when we are not in a view model constructor but in a simple public class with few functions, where we call the DependencyService.Get<IMessage>(). ShortAlert(“put a valid email”); because we are not able to get the singleton instance of the Imessage object…
How can we do it?
Thank you very much