CHAPTER 12
Navigation in Prism 4 defines how the user interface changes as the user interacts with the solution. Navigation covers a large number of situations. Following is a list of some of these situations.
Navigation in Prism 4 solutions is not as straight forward as in WPF solutions because of the necessity of integrating navigation with MVVM. Prism 4 supplies the tools needed to reduce the complexity of navigating in loosely coupled solutions.
Prism 4 uses two kinds of navigation.
Prism 4 View Based Navigation:
View based navigation is navigation that switches from one view to another or the addition or removal of a view to or from the visual tree.
In the Virtual Calculator solution Prism 4 navigation is used to add the Company logo and Application logo views to their respective regions in the shell form. The Math module uses view based navigation to add the two tabs (Add Two Integers and Subtract Two Integers) to the Syncfusion tab control. Listing 57 shows an example of loading the two Math module tabs.
Listing 57: Loading Math Views with Prism 4 Navigation:
[Module( ModuleName = "MATH_MODULE", OnDemand = false)] public class MATH_MODULE : IModule { private IRegionManager RegionManager { get; set; } private AddTwoView AddTwoView; private SubtractView SubtractView; //Constructor: public MATH_MODULE( IRegionManager RegionManager, AddTwoView AddTwoView, SubtractView SubtractView) { if (RegionManager != null) { this.RegionManager = RegionManager; } if (AddTwoView != null) { this.AddTwoView = AddTwoView; }
if (SubtractView != null) { this.SubtractView = SubtractView; } } //Add the Module Views to the Regions here: public void Initialize() { ///Add the user controls to the region here: IRegion MathRegion = RegionManager.Regions["MathRegion"]; MathRegion.Add(this.AddTwoView, "AddTwoView"); MathRegion.Add(SubtractView, "SubtractView"); } } |
The views and region manager are first instantiated by using constructor injection. Guard clauses are used to insure that the objects are not null, if they are not null, they are assigned to private members in the class. The initialize method creates a region (MathRegion) that will serve as the loading point for the two views. The two tabs (AddTwoView and SubtractView) are then added to the region.
This type of navigation is considered to be a limited form of Prism 4 navigation. The region class of Prism 4 has been extended to allow for more dynamic navigation between views. The previous example shows an example of loading the view automatically when the module is loaded. This type of view loading works fine when the module is being initialized, but how are views changed as the solution runs?
Using a Region Manager to Navigate:
The RequestNavigate method of the region manager class allows for navigation during runtime of the solution. Listing 58 shows an example of the RequestNavigate method.
Listing 58: Using IRegionManager to Navigate:
IRegionManager RegionManager = new RegionManager(); RegionManager.RequestNavigate("MathRegion", new Uri("AddTwoView", UriKind.Relative)); |
After creating a region manager, the RequestNavigate method of the region manager is called. Two arguments are passed to the method. The first argument is the name of the region that will be populated. The second argument is a URI the contains the name of the view that will populate the region and a UriKind enumeration.
A region can also be used to navigate to a view. Listing 59 shows an example of using a region to navigate.
Listing 59: Using IRegion to Navigate:
IRegion MathRegion = RegionManager.Regions["MathRegion"]; MathRegion.RequestNavigate(new Uri("AddTwoView", UriKind.Relative)); |
It is not necessary to pass the region name to the RequestNavigate method when a region is used because the region is known.
It is also possible to identify a callback method when navigating with the RequestNavigate method. This callback method will execute after the navigation completes Listing 60 shows an example of the RequestNavigate method with a callback.
Listing 59: Using IRegion to Navigate with a Callback Method:
IRegion MathRegion = RegionManager.Regions["MathRegion"]; MathRegion.RequestNavigate(new Uri("AddTwoView", UriKind.Relative), NavigationCompleted); private void NavigationCompleted(NavigationResult Result) { } |
The callback method is added as the last argument of the RequestNavigate method. A Method with the callback name is then created. This method must take a NavigationResult type as an argument.
Prism 4 State Based Navigation:
State based navigation is navigation that is implemented though state changes in the solution. The changes are made to the visual tree of the solution, though user interaction or by program code. State based navigation works best in the following scenarios.
The View Changes State Though User Interaction:
The Virtual Calculator solution uses a combination of XAML markup, view model and model code in the Math module to update the user interface depending on the current state of the module. Behaviors in the views are used to trigger specific states which are also located in the view as XAML markup. The states run animation storyboards which in turn set the properties of controls in the view. Each state has a unique set of properties that are set to specific values when the state is activated.
States are triggered from the view models of the module. As the user interacts with the user interface interaction triggers in the view fire when events are triggered. These triggers are associated with commands in the view model that determine the current state. The model exposes services from the Infrastructure project that actually apply the necessary validation and UI update business rules to reflect the current state.
The View Changes State Though Internal Code:
The view can also be updated by code that uses internal criteria to set states. Examples are timers, random number generators, date / time triggers and any other code based mechanism that does not depend on user interaction with the solution.
Existing Data Needs to Be Shown in Different Layouts:
It is sometimes necessary to show data in multiple ways. It may be for instance, necessary to show data in a tab view, carousel view and list view. As long as it is not necessary to update the data, state based navigation will work well. Prism 4 provides functionally that simplifies transitions between views when it is not necessary to interact between the view and the view model.
In chapter 7 we looked at some state based navigation constructs so we won't go into detail here. Revisit chapter seven if you need to see examples from the Virtual Calculator solution.
In this chapter we took a brief look at Prism 4 navigation. We learned that there are two kinds of navigation, view based navigation and state based navigation. We saw that view based navigation is used to switch views with view model interaction. We also learned that state based navigation is used to modify controls and views that already exist in the visual tree. We also saw that these state changes can be triggered though user interaction with the UI or by the solution's internal code.
In the next chapter we're going to take a closer look at the Virtual Calculator solution.