Ahmad ElMadi
Ahmad ElMadi

Reputation: 2617

Prism for Xamarin.Forms: I cannot resolve the navigationService unless if ViewModelLocator.AutowireViewModel="True"

Have been stuck for days ... and then I found what was the issue

I have two pages one in case of tablet and the other in case of phone . Since they have different names then using ViewModelLocator.AutowireViewModel to share the same view model as a binding context does not work . So for this case I am using the classic binding context, as i did not want to create a viewmodel copy for each page. My problem was when I run it gives me an error saying "no parameterless constructor defined for this object prism" The reason of this error is that the viewModel has a contructor that expects a navigationService. To solve this error i have to remove the navigation service as an argument , but then I need it so i can navigate with it .

Upvotes: 1

Views: 1064

Answers (1)

user5420778
user5420778

Reputation:

In this case I would recommend using the ViewModelLocationProvider.Register method. So in the application's RegisterTypes method, you can put a condition similar to this:

if (Device.Idiom == TargetIdiom.Tablet)
    ViewModelLocationProvider.Register("MainPage", () => your logic);
else
    ViewModelLocationProvider.Register("MainPage", () => your logic);

EDIT: Actually, you'll still have to manually resolve the INavigationService doing it this way. I am going to reopen your Prism Issue. We can look into improving the ViewModelocationProvider.Register method to enable this scenario.

Upvotes: 1

Related Questions