How do I make Calburn.Micro use my multi-parameter constructor?
When I have a View Model setup with an ImportingConstructor that takes a single parameter, the constructor is called and the screen is correctly shown. Example:
[ImportingConstructor]
public ShellViewModel(IEventAggregator eve开发者_JS百科nts)
{
events.Subscribe(this);
}
However, based on examples from Caliburn.Micro, it seems I should be able to provide a different constructor with N parameters. Example:
[ImportingConstructor]
public ShellViewModel(LeftViewModel left, RightViewModel right, IEventAggregator events)
{
Left = left;
Right = right;
events.Subscribe(this);
}
But this version of the constructor is never called in my sandbox. I have compiled and ran sample code from Caliburn.Micro that does this exact thing; calls a multiparameter constructor. (See Caliburn.Micro sample project "HelloEventAggregator")
Indeed - when I run my sandbox code (the second version) the constructor is not called, and a different ViewModel is selected as the initial display. But in the HelloEventAggregator sample their shellview model is constructed and displayed first.
What do I need to do to get Caliburn.Micro to call my multi-parameter constructor, and show the correct View?
The problem arrised because I had more than one view model set with
[Export(typeof(IShell))]
The export type should be whatever your [ImportingConstructor] is expecting, for that contract to be satisfied.
Example - ShellViewModel
[Export(typeof(IShell))]
public class ShellViewModel : PropertyChangedBase, IShell
{
[ImportingConstructor]
public ShellViewModel(LeftViewModel leftModel)
{
...
}
...
}
Example - LeftViewModel
[Export(typeof(LeftViewModel))]
public class LeftViewModel : PropertyChangedBase, IShell
{
[ImportingConstructor]
public LeftViewModel(IEventAggregator events)
{
events.Subscribe(this);
...
}
...
}
精彩评论