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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:02:26+00:00 2026-05-16T17:02:26+00:00

I am writing an WPF MVVM application using Prism. A couple days ago I

  • 0

I am writing an WPF MVVM application using Prism. A couple days ago I asked about best practices for managing different views and didn’t get a whole lot of feedback. Sense then I have come up with a system that seems to work, but I want to make sure I wont get bit down the road.

I followed the tutorials at http://development-guides.silverbaylabs.org/ to get my shell setup and am confident that my modules are being registered well.

However, nowhere in those tutorials was an example of a view being replaced with a different view within a given region. This in general, seems to be fairly hard to find a good example of. So, today I rolled my own solution to the problem.

Essentially the module has a controller that keeps track of the current view, then when the user wants to switch views, it calls the Regions.Remove command and then the add command to replace it with the current view. It seems like there must be a more elegant solution to just switch between different registered views, but I haven’t found it.

All the different possible views for a module are registered with the Unity container when the module is initialized.

The controller and the view switching function follows:

namespace HazardModule
{
    public class HazardController : IHazardController
    {

        private object CurrentView;
        public IRegionManager RegionManager { get; set; }
        private IUnityContainer _container;

        public HazardController(IUnityContainer container)
        {
            _container = container;
        }

        /// <summary>
        /// Switches the MainRegion view to a different view
        /// </summary>
        /// <typeparam name="T">The class of the view to switch to</typeparam>
        public void SiwthToView<T>()
        {
            if (CurrentView != null)
            {
                RegionManager.Regions["MainRegion"].Remove(CurrentView);
            }
            CurrentView = _container.Resolve<T>();
            RegionManager.Regions["MainRegion"].Add(CurrentView);
        }

    }
}

Any feedback or other better solutions would be appreciated.

  • 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-16T17:02:27+00:00Added an answer on May 16, 2026 at 5:02 pm

    I have pretty much the same approach, so does a co-worker who has a bit more Prism experience than myself.

    Basically I have a ViewController class which is a property in my ViewModelBase class. This enables all my ViewModels to have access to it in one go. Then in my ViewController class I have a few display management methods. The correctness of this approach is probably debatable but I found it to work quite well in my case

        public TView ShowViewInRegion<TView>(string regionName, string viewName, bool removeAllViewsFromRegion)
        {
            var region = regionManager.Regions[regionName];
    
            var view = region.GetView(viewName) ?? container.Resolve<TView>();
    
            if (removeAllViewsFromRegion)
            {
                RemoveViewsFromRegion(region);
            }
    
            region.Add(view, viewName);
            region.Activate(view);
    
            if (regionName == RegionNames.OverlayRegion)
            {
                eventAggregator.GetEvent<PopupWindowVisibility>().Publish(true);
            }
    
            return (TView)view;
        }
    
        public void RemoveViewsFromRegion(string regionName)
        {
            RemoveViewsFromRegion(regionManager.Regions[regionName]);
        }
    
        private void RemoveViewsFromRegion(IRegion region)
        {
            for (int i = 0; i < region.Views.Count() + i; i++)
            {
                var view = region.Views.ElementAt(0);
                region.Remove(view);
            }
    
            if (region.Name == RegionNames.OverlayRegion)
            {
                eventAggregator.GetEvent<PopupWindowVisibility>().Publish(false);
            }
        }
    
        private static void DeactivateViewsInRegion(IRegion region)
        {
            for (var i = 0; i < region.ActiveViews.Count(); i++)
            {
                var view = region.ActiveViews.ElementAt(i);
                region.Deactivate(view);
            }
        }
    

    Then whenever I need to switch out a view or whatever I can just call from my ViewModel

        public void ExecuteCreateUserCommand()
        {
            ViewController.ShowViewInRegion<IUserCreateView>(RegionNames.ContentRegion, ViewNames.UserCreateView, true);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a PRISM/MVVM/WPF application. It's a LOB application, so there are a
I'm writing a WPF application using the MVVM pattern, based on the following article:
I'm writing an application in WPF using the MVVM pattern. In my application I've
I'm writing an application in WPF using the MVVM-pattern and will really often use
I'm writting an WPF application using the mvvm toolkint. In the main windows I
I am writing a fairly simple WPF desktop application and under build\configuration manager, release
Writing my first Linq application, and I'm trying to find the best way to
I am new to WPF and MVVM, and I am working on an application
I'm working in WPF using the MVVM pattern, and generally things seem to be
I have a small WPF application based on MVVM priniciples. So far I had

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.