Reputation: 759
I have a question about the way Views are loaded. I have this sample code shown below of a view that loads correctly:
[ViewExport(RegionName = RegionNames.LeftRegion)]
[PartCreationPolicy(CreationPolicy.Shared)]
public partial class EmployeeListView : UserControl
{
[ImportingConstructor]
public EmployeeListView(EmployeeListViewModel viewModel)
{
InitializeComponent();
//this.DataContext = viewModel;
}
[Import]
public EmployeeListViewModel Model
{
get
{
return DataContext as EmployeeListViewModel;
}
set
{
DataContext = value;
}
}
}
Notes about above code:
However, in the Stocktrader demo the loading of the views is done differently. Look for example at the PositionSummaryView.xaml.cs file.
[ViewExport(RegionName = RegionNames.MainRegion)]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class PositionSummaryView : UserControl
{
public PositionSummaryView()
{
InitializeComponent();
}
#region IPositionSummaryView Members
[Import]
public IPositionSummaryViewModel Model
{
get
{
return DataContext as IPositionSummaryViewModel;
}
set
{
DataContext = value;
}
}
#endregion
}
My questions are the following:
Upvotes: 2
Views: 566
Reputation: 61589
There is little point on adding [ImportingConstructor]
to your constructor, if you don't intend to do anything with that in your constructor. On the flip side, if your instance of EmployeeListViewModel
is a required dependency for EmployeeListView
, then you probably should be passing it in via the constructor.
Having dual imports will cause the following:
[ImportingConstructor]
attribute allowing the instance of EmployeeListViewModel
to be injected.Model
will be injected with an instance of EmployeeListViewModel
after the type has been instantiated.Should EmployeeListViewModel
do any heavy lifting during instantiation (e.g. accessing a database or service), and is created as a non-shared part ([PartCreationPolicy(CreationPolicy.NonShared)]
), then you are unnecessarily instantiating the part twice and doubling your work. If the part is shared, this is less of an issue.
In answer to your questions though,
PositionSummaryView() { }
) for any types that do not have a constructor marked with ImportingConstructorAttribute
. So in the case of the PositionSummaryView
, it will check for an [ImportingConstructor]
decoration attribute, will not find it, so will use the default constructor.[Import]
is satisfied after the type has been constructed. I'd be surprised that the breakpoint is not being hit though... I'd check your build mode (Debug | Release
), etc.Upvotes: 2