Xamarin Prism Unable to navigate to LoginPageViewModel

1.1k views Asked by At

I am trying to make my Xamarin Project use MVVM with Prism and DryIoc.

I mostly want to use AutoRegistration like below:

[AutoRegisterForNavigation] 
...
protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
//Pages
 containerRegistry.RegisterForNavigation<NavigationPage>();

//Services
containerRegistry.RegisterSingleton<ILocalDatabase, LocalDatabase>();
containerRegistry.RegisterSingleton<IUserProfileDataStore, UserProfileDataStore>();
containerRegistry.RegisterSingleton<IApplicationSettings, ApplicationSettings>();
containerRegistry.RegisterSingleton<ILogger, Logger>();
containerRegistry.RegisterSingleton<IApiService, ApiService>();
containerRegistry.RegisterSingleton<IUserSession, UserSession>();
containerRegistry.Register<IBrowser, BrowserImplementation>();
containerRegistry.Register<IConnectivity, ConnectivityImplementation();
containerRegistry.Register<IFileSystem, FileSystemImplementation>();
containerRegistry.Register<ICoreServices, CoreServices>();
}

I have also tried Manual Registration:

 containerRegistry.RegisterForNavigation<LoginPage, LoginPageViewModel>();

Neither works, It hits the Login Page code behind then breaks with the following error:

Exception - High: Prism.Ioc.ContainerResolutionException: 
An unexpected error occurred while resolving 'AppetiteApp.ViewModels.LoginPageViewModel' ---> 
DryIoc.ContainerException: code: UnableToResolveUnknownService; message: Unable to resolve 
Resolution root AppetiteApp.ViewModels.LoginPageViewModel 
with passed arguments [value(Prism.Navigation.ErrorReportingNavigationService)]

**System.NullReferenceException:** 'Object reference not set to an instance of an object.'

I've also tried using a Linker file setting it's build action to "linkdescription"

As for my Login Page here is the declaration

   public LoginPageViewModel(ICoreServices coreServices)
            : base(coreServices)
2

There are 2 answers

0
Nathan Scullen On BEST ANSWER

So Once I investigated Inside of ICoreServices I commented out each of the dependencies then discovered that IUserSession was the once causing problems then I dug into that and discovered that the dependences for IAppInfo and IVersionTracking were missing in the App.Xaml.cs registr tyepes so I added that and then it worked!

containerRegistry.Register<IAppInfo, AppInfoImplementation>();
containerRegistry.Register<IVersionTracking, VersionTrackingImplementation>();

0
dadhi On

The constructor of LoginPageViewModel requires the ICoreServices argument which is registered.

The error message says that the LoginPageViewModel itself is unknown to the IoC - it means the type LoginPageViewModel is not directly registered and not found through dynamic registrations or unknown service resolvers.

I am not a user of the Xamarin Prism so I am not sure about its mechanism for registering the view models.

Btw, this part of error

Resolution root AppetiteApp.ViewModels.LoginPageViewModel with passed arguments [value(Prism.Navigation.ErrorReportingNavigationService)]

basically means the view-model was resolved via the foollowing call resolver.Resolve(typeof(LoginPageViewModel), args: new[] { errorReportingNavigationService })

Hope it will help you or someone knowledgeable in Xamarin to track the error cause.