Request Navigate In PRISM

Prism is a framework for building loosely coupled, maintainable, and testable XAML applications in WPF, Windows 10 UWP, and Xamarin Forms.
 
RequestNavigate method was introduced in Prism 6.
 
In this tutorial, I am going to be creating a simple project to demonstrate how to use the prism RequestNavigate method to navigate between two views in a Region.
 
I assume you already have  knowledge of {rism and MVVM so go ahead and create a new WPF Application and name it PrismDemo.
 
Add reference of the Prism library to the application from Nuget package as shown below,
 
Request Navigate In PRISM
 
Delete the MainWindow.xaml and add a new Window called Shell.xaml.
 
The User interface of the Shell.xaml  will be very simple consisting of two buttons that will be used to navigate between views and a ContentControl that will serve as the region for the views and the region name is MainRegion.
  1. <Window x:Class="PrismDemo.Shell"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6.         xmlns:local="clr-namespace:PrismDemo"  
  7.         mc:Ignorable="d"  
  8.         xmlns:prism ="http://prismlibrary.com/"  
  9.         Title="Shell" Height="300" Width="300">  
  10.     <Grid>  
  11.         <Grid.RowDefinitions>  
  12.             <RowDefinition Height="auto"/>  
  13.             <RowDefinition Height="*"/>  
  14.         </Grid.RowDefinitions>  
  15.         <Grid Grid.Row="0">  
  16.             <Grid.ColumnDefinitions>  
  17.                 <ColumnDefinition Width="auto"/>  
  18.                 <ColumnDefinition Width="auto"/>  
  19.             </Grid.ColumnDefinitions>  
  20.             <Button Width="75" Height="30" Command="{Binding ViewACommand}" Content="View A" Margin="20,20,20,0" Grid.Column="0"/>  
  21.             <Button Width="75" Height="30" Command="{Binding ViewBCommand}" Content="View B" Margin="20,20,20,0" Grid.Column="1"/>  
  22.         </Grid>  
  23.         <ContentControl Grid.Row="1" prism:RegionManager.RegionName="MainRegion"/>  
  24.     </Grid>  
  25. </Window>  
Request Navigate In PRISM
 
It is now time for the Bootstrapper class.
 
Create a new class called the Bootstrapper.cs and paste the code below.
  1. using Microsoft.Practices.Unity;  
  2. using Prism.Unity;  
  3. using System.Windows;  
  4.   
  5. namespace PrismDemo  
  6. {  
  7.     public class Bootstrapper: UnityBootstrapper  
  8.     {  
  9.         protected override void InitializeShell()  
  10.         {  
  11.             base.InitializeShell();  
  12.             Application.Current.MainWindow.Show();  
  13.         }  
  14.         protected override DependencyObject CreateShell()  
  15.         {  
  16.             return Container.Resolve<Shell>();  
  17.   
  18.         }  
  19.   
  20.         protected override void ConfigureContainer()  
  21.         {  
  22.             base.ConfigureContainer();  
  23.             Container.RegisterTypeForNavigation<ViewA>("ViewA");  
  24.             Container.RegisterTypeForNavigation<ViewB>("ViewB");  
  25.         }  
  26.     }  
  27. }  
From the code above, the views to be navigated to are registered in the ConfigureContainer method using the RegisterTypeForNavigation method which is an extension method in Prism.Unity.
 
Request Navigate In PRISM
 
Create another class called the ViewModel.cs which will be the viewmodel for the Shell.xaml, copy and paste the code below.
  1. using Microsoft.Practices.ServiceLocation;  
  2. using Prism.Commands;  
  3. using Prism.Regions;  
  4.   
  5. namespace PrismDemo  
  6. {  
  7.     public class ViewModel  
  8.     {  
  9.         public ViewModel()  
  10.         {  
  11.             ViewACommand = new DelegateCommand(ViewAAction);  
  12.             ViewBCommand = new DelegateCommand(ViewBAction);  
  13.         }  
  14.   
  15.   
  16.         private void ViewAAction()  
  17.         {  
  18.             RegionManger.RequestNavigate("MainRegion""ViewA");  
  19.         }  
  20.         private void ViewBAction()  
  21.         {  
  22.             RegionManger.RequestNavigate("MainRegion""ViewB");  
  23.         }  
  24.   
  25.         public DelegateCommand ViewACommand { get; }  
  26.         public DelegateCommand ViewBCommand { get; }  
  27.         IRegionManager RegionManger { get { return ServiceLocator.Current.GetInstance<IRegionManager>(); } }  
  28.     }  
  29. }  
The ViewModel.cs consist of the two DelegateCommand Property that we will bind to the buttons in the Shell.
 
When any button is clicked, the ViewAAction() or ViewBAction() method is executed.
 
The method calls the RequestNavigate method which in our case takes in two parameters which are the RegionName and the name of the View to navigate to.
 
Set the DataContent of the Shell to ViewModel.cs as shown below
  1. using System.Windows;  
  2.   
  3. namespace PrismDemo  
  4. {  
  5.     /// <summary>  
  6.     /// Interaction logic for Shell.xaml  
  7.     /// </summary>  
  8.     public partial class Shell : Window  
  9.     {  
  10.         public Shell()  
  11.         {  
  12.             InitializeComponent();  
  13.             DataContext = new ViewModel();  
  14.         }  
  15.     }  
  16. }  
Lastly, go to the App.xaml and remove this line of code.
  1. StartupUri="MainWindow.xaml"  
And in the App.xaml.cs override the OnStartUp Method.
  1. using System.Windows;  
  2.   
  3. namespace PrismDemo  
  4. {  
  5.     /// <summary>  
  6.     /// Interaction logic for App.xaml  
  7.     /// </summary>  
  8.     public partial class App : Application  
  9.     {  
  10.         protected override void OnStartup(StartupEventArgs e)  
  11.         {  
  12.             new Bootstrapper().Run();  
  13.         }  
  14.     }  
  15. }  
Now run the application and you see something like these:
 
Request Navigate In PRISM
 
Request Navigate In PRISM