Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8264331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:25:06+00:00 2026-06-08T04:25:06+00:00

I built a little view locator. public abstract class ViewModelLocatorBase : IViewModelLocator { private

  • 0

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

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:25:07+00:00Added an answer on June 8, 2026 at 4:25 am

    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

    Rule Description from MSDN :

    Constructors on abstract types can be called only by derived types.
    Because public constructors create instances of a type, and you cannot
    create instances of an abstract type, an abstract type that has a
    public constructor is incorrectly designed.

    So you can use the contructor in you abstrat class as protected this way

    public abstract class ViewBase : UserControl
    {
        protected ViewBase()
        {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my little scratch built flex game framework, I have defined a class called
Ive built a little jquery sliding div on my website that when you hover
I built a little installer, which installs to program files. I run the installer
I have built a little web application with several *.js files. I am using
i tried to built up a little Google Maps api in PHP which fetches
I've built a web site on ASP.NET MVC and one little section of it
We have a little in-house LMS with courses built in Flash. Scores are updated
I've got an little canvas tool I've built that cut up images into little
I am working on a B2B site built in Magento which is a little
I'm using the Ruby SVN bindings built with SWIG. Here's a little tutorial. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.