MVVM Fabric: Using a View Placer

A while back, I wrote about the navigation solution provided by MVVM Fabric.  I also wrote about using the ModalViewPlacer provided by MVVM Fabric.  In this post, I will talk about how to provide your own implementation of the IViewPlacer interface.

Overview

Since each application is unique, with its own unique UI, MVVM Fabric leaves it up to you, the developer, to provide the mechanism to place a view into that unique UI.  IVewPlacer is that mechanism.

An implementation of the IViewPlacer interface is required by the navigation solution provided by MVVM Fabric.  It will be used by the ViewController to place a view into your application when a request is made to navigate to a view.

A First Look

The IViewPlacer interface provided by MVVM Fabric follows the single responsibility principle.  Its only job is to place a view into your application, which you can see by looking at the interface itself.

When you implement IViewPlacer, you must provide an implementation for the PlaceView method.  The PlaceView method takes a ViewResult as a parameter.  So, let’s look at the ViewResult class as well.

As you can see, the ViewResult class has two properties.  The first property is the actual instance of the view, as created by the view factory.  This view will have a fully loaded view model and is all ready to make an entrance into your UI.  The second property is the ViewTargets value for that view.  There may be times where you need to place certain views in different places than the rest of the views.  With the ViewTargets property on ViewResult, you know what view you are placing and can act accordingly.

IViewPlacer Implementation

The sample application provided with the MVVM Fabric source has an implementation of the IViewPlacer interface.  We will be looking at a simplified version of that implementation.

The sample application has a tabbed user interface, so the view placer needs to take a view and place it into the TabControl used by the main view.  As you can see, it cycles through any existing views so that duplicates are not shown.  Then, if the view is not a duplicate, it is placed into the TabControl and given focus.

This is a simple UI, so a fairly simple view placer is needed.  More complicated UIs will likely need more complicated view placers.  As an example, a previous project I worked on used a docking solution similar to Visual Studio.  In that case, the view placer would need to be aware of what views go into what docking sections.

Conclusion

The IViewPlacer interface is integral to the navigation solution provided by MVVM Fabric.  Since each application’s user interface is different, it is left up to the consumer of the library to implement this interface.  The complexity of the view placer implementation is dependent on the complexity of the user interface, but placing the view is its only responsibility.

Share