MAUI DependencyService.Get returning null

Ivano Panichelli 1 Punto di reputazione
2025-05-28T23:50:54.2833333+00:00

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

Tecnologie per sviluppatori | Visual Studio | Altro
0 commenti Nessun commento
{count} voti

Risposta

Le risposte possono essere contrassegnate come risposte accettate dall'autore della domanda. Ciò consente agli utenti di sapere che la risposta ha risolto il problema dell'autore.