我目前正在使用MahApps Metro学习Catel + Orchestra。 我正在使用MetroUI从Catel.Examples项目中执行身份验证示例。 我的问题是我在MahAppsService中创建一个新的MainWindow
public FrameworkElement GetMainView()
{
return new MainWindow();
}
永远不会调用MainWindowViewModel的构造函数
public MainWindowViewModel(UIVisualizerService uiVisualizarService, IAuthenticationProvider authenticationProvider)
{
_uiVisualizerService = uiVisualizarService;
_authenticationProvider = authenticationProvider;
RoleCollection = new ObservableCollection<string>(new[] { "Read-Only", "Administrator" });
ShowView = new Command(OnShowViewExecute, OnShowViewCanExecute, "ShowView");
}
我已将其缩小到构造函数的2个依赖项。如果我删除了UIVisualizerService和IAuthenticacionProvider依赖项,则正确调用构造函数,但ModelView稍后需要这两个服务。
我迷失了我能做些什么才能让它发挥作用。
答案 0 :(得分:1)
您必须在ServiceLocator中注册IAuthenticationProvider:
var serviceLocator = ServiceLocator.Default;
serviceLocator.RegisterType<IAuthenticationProvider, MyAuthenticationProvider>();
请注意,Catel中的所有服务都会自动为您注册,但您必须自己注册自己的服务(例如,使用ModuleInit或程序集中的其他入口点)。
答案 1 :(得分:0)
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<MainWindowViewModel>(_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.