I'm trying to register a service at the SimpleIOC that comes with MVVMLight. I have the ViewModelLocator in a PCL. I want to register a service from within my main project.
I'm working on Windows Phone 8.1 Silverlight and try to implement/register a navigation service.
public interface INavigationService
{
void Navigate(string uri);
void Navigate(string uri, Dictionary<string, string> parameters);
void GoBack();
}
Implementation of my service interface:
public class NavigationService : INavigationService
{
public NavigationService()
{
}
public void Navigate(string uri)
{
DispatcherHelper.UIDispatcher.BeginInvoke(
() => ((PhoneApplicationFrame) Application.Current.RootVisual).Navigate(new Uri(uri, UriKind.Relative)));
}
public void Navigate(string uri, Dictionary<string, string> parameters)
{
throw new NotImplementedException();
}
public void GoBack()
{
DispatcherHelper.UIDispatcher.BeginInvoke(
() => ((PhoneApplicationFrame) Application.Current.RootVisual).GoBack());
}
}
This is how I register the service at the IOC container from within my App.xaml.cs (OnLaunching method)
// init the dispatcher helper for MVVM usage
DispatcherHelper.Initialize();
// add some platform specific services to the IOC container
SimpleIoc.Default.Register<INavigationService, NavigationService>();
And this is the error message that I can when starting my app:
Microsoft.Practices.ServiceLocation.ActivationException: Cannot register: No public constructor found in NavigationService.
at GalaSoft.MvvmLight.Ioc.SimpleIoc.GetConstructorInfo(Type serviceType)
at GalaSoft.MvvmLight.Ioc.SimpleIoc.Register[TInterface,TClass](Boolean createInstanceImmediately)
at GalaSoft.MvvmLight.Ioc.SimpleIoc.Register[TInterface,TClass]()
at TimeStamp.WindowsPhone.App.Application_Launching(Object sender, LaunchingEventArgs e)
at Microsoft.Phone.Shell.PhoneApplicationService.FireLaunching()
at Microsoft.Phone.TaskModel.Interop.Task.FireOnLaunching()
As I have a constructor in my NavigationService it seems that something obvious which I don't see is missing here.
Btw.: I want to use that service from within my main view model:
/// <summary>
/// Default constructor
/// </summary>
/// <param name="workDayServiceAgent"></param>
[PreferredConstructor]
public MainViewModel(IWorkDayServiceAgent workDayServiceAgent, INavigationService navigationService)
{
// set the service agents
_workDayServiceAgent = workDayServiceAgent;
_navigationService = navigationService;
...
After more than two hours of more research in my code I figured out what the actual issue is. When registering NavigationService I'm actually not using my own implementation but System.Windows.Navigation.NavigationService. After renaming my NavigationService to NavigationServiceWP everything works fine.