I built a little view locator.
public abstract class ViewModelLocatorBase : IViewModelLocator
{
private readonly static bool isExecutingInDesignMode =
DesignMode.DesignModeEnabled;
public IViewModel ViewModel
{
get { return isExecutingInDesignMode
? LocateDesignTimeViewModel()
: LocateRuntimeViewModel(); }
}
protected abstract IViewModel LocateDesignTimeViewModel();
protected abstract IViewModel LocateRuntimeViewModel();
}
Which is used to build more specific view locator’s
public class UserEditorViewModelLocator : ViewModelLocatorBase
{
protected override IViewModel LocateDesignTimeViewModel()
{
return new UserEditorViewModelDesignTime();
}
protected override IViewModel LocateRuntimeViewModel()
{
return new UserEditorViewModelRunTime();
}
}
These are used by my views to locate the correct view model
public abstract class ViewBase : UserControl
{
public ViewBase()
{
BindViewModelLocatorToView(viewModelLocator: GetViewModelLocator());
}
protected abstract IViewModelLocator GetViewModelLocator();
protected void BindViewModelLocatorToView(IViewModelLocator viewModelLocator)
{
if (viewModelLocator != null)
{
DataContext = viewModelLocator.ViewModel;
}
}
}
By providing the correct view locator in the derived view (eventually inject via IoC)
public sealed partial class UserEditorScreen : ViewBase
{
public UserEditorScreen()
{
this.InitializeComponent();
}
protected override IViewModelLocator GetViewModelLocator()
{
return new UserEditorViewModelLocator();
}
}
Now, this all works perfectly in run time but in design time the View breaks because of the call to BindViewModelLocatorToView. I had been using these view locator’s directly in Xaml as StaticResources so they do seem to work that way for design time but since the change to populating the DataContext in the constructor of the view I am missing the design time ViewModel.
The Error

In C# abstract class cannot have a public constuctor and is an violation of abstract rule
See MSDN here http://msdn.microsoft.com/en-us/library/ms182126(v=vs.100).aspx
So you can use the contructor in you abstrat class as protected this way