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 4611132
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:13:36+00:00 2026-05-22T01:13:36+00:00

Technologies C# 4.0 Prism 4 with Unity for DI WPF MVVM Preface There are

  • 0

Technologies

  • C# 4.0
  • Prism 4 with Unity for DI
  • WPF
  • MVVM

Preface

There are two projects in my solution, MyApp.Shell and MyApp.ModuleFoo

MyApp.Shell’s Unity Bootstrapper

protected override IModuleCatalog CreateModuleCatalog()
{
    // Module assemblies are read from a directory.
    DirectoryModuleCatalog moduleCatalog = new DirectoryModuleCatalog();
    moduleCatalog.ModulePath = @".\Modules";
    return moduleCatalog;
}

The project MyApp.ModuleFoo contains a View and a View Model.

The ViewModel

// Somehow, Unity sees this class and registers the type.
public class FooViewModel : ViewModelBaseClass
{
    public string FooText
    {
        get { return "Foo!"; }
    }
}

The View

<Label Content={Binding FooText} />

The View’s Code-behind

// Unity automatically sees this as Constructor Injection, 
// which is exactly what I desire.
public FooView(FooViewModel viewModel)
{
    DataContext = viewModel;
    ...
}

MyApp.FooModule’s Initialization

Perhaps registering FooView with the region manager is inadvertently registering FooViewModel with Unity?

public void Initialize()
{
    var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
    regionManager.RegisterViewWithRegion("FooRegion", typeof(FooView));
}

The view correctly displays “Foo!”.

Problems

  • How do I tell Unity to register only a single instance of FooViewModel?
  • Additionally, (and I’m thinking ahead here), how would I tell unity not to register FooViewModel?

Thanks for the help.

Edit:

Added MyApp.FooModule’s Initialization code

Edit (Solution):

It turns out RegisterViewWithRegion has two overloads. From Prism’s documentation, when the overload I’m using is used, a new instance of the view is created. I’m assuming this also creates a new instance of FooViewModel.

The other overload uses a delegate to resolve FooView. The documentation says this overload is used in the “ViewModel-first” approach. I’m going to make this question as answered, but if anyone has any additional insight, I’d love to hear.

  • 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-05-22T01:13:37+00:00Added an answer on May 22, 2026 at 1:13 am

    // Somehow, Unity sees this class and registers the type.
    public class FooViewModel : ViewModelBaseClass

    …

    I am surprised that you say this as Unity does not register types inside the container by default. You have to tell it to do so either programmatically or in the config file.

    When you have concrete classes (not interfaces) they will automatically get created by Unity whether they are registered or not. If not the default behavior is to create a new instance each time. No lifetime management is applied also.

    As far as your questions:

    To register only one type within your initialisation of your module just have.

    Container.RegisterType<FooViewModel>(new ContainerControlledLifetimeManager());
    

    The lifetime manager will instruct unity to only create one instance of the view model.

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

Sidebar

Related Questions

I'm developing a WPF front end (using MVVM and Prism) for an existing MFC
There are several similar Oracle technologies - Oracle Streams, Oracle Change Data Capture and
Learning java server technologies, trying to clarify some things. There are few technologies that
As with most new technologies after a while a standard emerges. Is there anything
There are many more technologies and tools available to build the front end for
I am a little confused about two new technologies receiving a lot of buzz
maybe using the latest technologies, in 3.5 SP1, is there a way to short
Was there concurrent technologies at that time? What was advantages of OSGi compared to
Has anyone had experience using these two technologies in tandem? What are (if any)
Coming from some other technologies, there have been string comparison assertions for use in

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.