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

  • Home
  • SEARCH
  • 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 8357309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:26:14+00:00 2026-06-09T10:26:14+00:00

I have two ViewModels: MainViewModel and QuestionViewModel. I Register they in ViewModelLocator. SimpleIoc.Default.Register<MainViewModel>(); SimpleIoc.Default.Register<QuestionViewModel>();

  • 0

I have two ViewModels: MainViewModel and QuestionViewModel. I Register they in ViewModelLocator.

SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<QuestionViewModel>();

On MainViewModel I have a ListBox with Questions. When Click I execute this command

NavigationService.NavigateTo(new Uri("/Pages/QuestionPage.xaml", UriKind.Relative));
Messenger.Default.Send<Question, QuestionViewModel>(q);

QuestionPage’s DataContext set to QuestionViewModel. On QuestionViewModel I register message:

Messenger.Default.Register<Question>(this, q =>
{
    MessageBox.Show("!");
});

But this function work only on second open of QuestionViewModel, because QuestionViewModel instance creates only on first opening. My Question is: Where and When I need to create instance of QuestionViewModel in MVVM Light? I dont want create all my pages at startup.

Now, I create instance in constructor ViewModelLocator:

public ViewModelLocator()
{
    ServiceLocator.Current.GetInstance<QuestionViewModel>();
}

It is good practice?

  • 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-09T10:26:15+00:00Added an answer on June 9, 2026 at 10:26 am

    You’re right, this is not a good practice to create all view models in ViewModelLocator constructor. The reason of the issue you got is that QuestionViewModel is created after you send the message. You can try to create a service which will share current Question object and inject this service into MainViewModel and QuestionViewModel.

    public interface IQuestionService
    {
        Question CurrentQuestion {get; set;}
    }
    
    public class QuestionService : IQuestionService
    {
        public Question CurrentQuestion {get; set;}
    }
    

    Then on Click in MainViewModel just save current question:

    _questionService.CurrentQuestion = q;
    

    and use in in your QuestionViewModel

    Just make sure you inject the same instance of IQuestionService into your view models.

    One more variant is to pass simple navigation arguments in a URL like this:

    NavigationService.NavigateTo(new Uri("/Pages/QuestionPage.xaml?questionid=" + q.Id, UriKind.Relative));
    

    Alternatively, you could try to implement your own NavigationService which supports passing parameters, but this is more complicated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three ViewModels: QuestionViewModel Theese two inherit from QuestionViewModel MCQViewModel MatrixViewModel I send
I have two columns say Main and Sub . (they can be of same
I have two ViewModels that present the same Model to different Views. One presents
I Have an MVC Project roughly organized like this: Project 1: MVC app (ViewModels,
So I have this two classes: public class PhysicalTest { public int ID {
I have a situation where I need to see if two viewmodels derive from
I have come accross two ways of initializing Views and ViewModels in WPF CAL
I have two ViewModels which are similar, and i need to cast one to
I have two ViewModels in my MVVM app. the one is bind to main
I have two ViewModels: public class CommandViewModel { public string DisplayName { get; set;

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.