views:

80

answers:

1

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.

A: 

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);
    }
WiT8litZ