Catel authentication + MahApps

292 Views Asked by At

I am currently learning Catel+Orchestra using MahApps Metro. I am doing the Authentication example from the Catel.Examples project using the MetroUI. My problem is when i create a new MainWindow in my MahAppsService

        public FrameworkElement GetMainView()
    {
        return new MainWindow();
    }

The constructor of the MainWindowViewModel is never called

public MainWindowViewModel(UIVisualizerService uiVisualizarService, IAuthenticationProvider authenticationProvider)
    {
        _uiVisualizerService = uiVisualizarService;
        _authenticationProvider = authenticationProvider;
        RoleCollection = new ObservableCollection<string>(new[] { "Read-Only", "Administrator" });
        ShowView = new Command(OnShowViewExecute, OnShowViewCanExecute, "ShowView");
    }

I have narrowed it down to the 2 dependencies of the constructor. If i remove the UIVisualizerService and IAuthenticacionProvider dependencies the constructor is properly called but the ModelView needs those two services later on.

I am lost at what can i do to get this working.

2

There are 2 best solutions below

0
Salvador Ruiz Guevara On BEST ANSWER

I solved the problem by adding a explicit injection of the viewmodel into the mainwindow constructor.

public MainWindow(MainWindowViewModel _mainwindowviewmodel):base(_mainwindowviewmodel)
 {
 InitializeComponent();
 }

Declaring the field for the AuthenticationProvider interface to the MahAppsService class.

private readonly IAuthenticationProvider _authenticationProvider;

Also adding the dependency of the AuthenticationProvider interface to the constructor.

 public MahAppsService(ICommandManager commandManager, IMessageService messageService, IUIVisualizerService uiVisualizerService, IAuthenticationProvider authenticationProvicer)
    {
        Argument.IsNotNull(() => commandManager);
        Argument.IsNotNull(() => messageService);
        Argument.IsNotNull(() => uiVisualizerService);
        Argument.IsNotNull(() => authenticationProvicer);
        _commandManager = commandManager;
        _messageService = messageService;
        _uiVisualizerService = uiVisualizerService;
        _authenticationProvider = authenticationProvicer;
    }

And the last step is creating an instance of the viewmodel in the GetMainView in the MahAppsService class.

 public FrameworkElement GetMainView()
 {
 var mainwindowViewModel = TypeFactory.Default.CreateInstanceWithParametersAndAutoCompletion<MainWindowView‌​Model>(_uiVisualizerService, _authenticationProvider);
 return new MainWindow(mainwindowViewModel);
 }

Please note that this might not be the best way to do it but it gets the work done. If someone has better way feel free to share it.

7
Geert van Horrik On

You must register the IAuthenticationProvider in the ServiceLocator:

var serviceLocator = ServiceLocator.Default;

serviceLocator.RegisterType<IAuthenticationProvider, MyAuthenticationProvider>();

Note that all services inside Catel are automatically registered for you, but you must register your own services yourself (for example, by using ModuleInit or another entry point in your assembly).