I'm attempting to learn Prism and utilize in in a new application I'm working on but am stuck at trying to get it to switch content in navigation. For reference, the video that I was working/learning from was Brian Lagunas' video MVVM Made Simple on YouTube (I've gone through the section multiple times but can't figure out what I'm doing incorrectly). Here is the code behind for my main window (when I added the regionManager.RegisterViewWithRegion()
line it allowed the first view registered to show upon launch but still will not allow me to switch from one view to the other.
public partial class MainWindow : Window
{
protected RegionManager regionManager = new RegionManager();
public MainWindow()
{
InitializeComponent();
regionManager.RegisterViewWithRegion("Content", typeof(Index));
regionManager.RegisterViewWithRegion("Content", typeof(Home));
DataContext = new MainWindowViewModel(this, regionManager);
This is all the code and the prism xmlns statements (these are at the top of all of my views) that I have related to the region/region manager - I've tested the Command binding on the button and know that the method is being triggered so the buttons should be good - but everything else I'm unsure of but per the video it looks correct to me.
XMLNS statements
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
Content region in XAML
<!-- Page Content -->
<Border Grid.Row="3" Padding="{Binding WindowContentPadding}">
<ContentControl prism:RegionManager.RegionName="Content" />
</Border>
Bootstrapper
class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void InitializeShell()
{
Application.Current.MainWindow.Show();
}
protected override void ConfigureContainer()
{
base.ConfigureContainer();
// Registering Unity containers for view model navigation
// All views must be registered.
Container.RegisterType(typeof(object), typeof(Home), "Home");
Container.RegisterType(typeof(object), typeof(Index), "Index");
}
}
MainWindowViewModel
public DelegateCommand<string> NavigateCommand { get; set; }
public MainWindowViewModel(Window _window, RegionManager _regionManager)
{
window = _window;
// MVVM model locator code
regionManager = _regionManager;
NavigateCommand = new DelegateCommand<string>(Navigate);
}
private void Navigate(string uri)
{
//Test
Console.WriteLine(uri);
regionManager.RequestNavigate("Content", uri);
}
I've also checked his MVVM Pluralsight course and the instructions look the same there as well so from what I can tell so it must be something I am missing.
protected RegionManager regionManager = new RegionManager();
Neither do you want to create your own RegionManager
nor do you want to do it within a view.
Perform your navigation within the view model and have the region manager injected.
public MainWindowViewModel(Window _window, RegionManager _regionManager)
You don't want to have the view inside the view model and you don't need it anyway.
Have the view interact with the view model through bindings only.
I have two views (viewA and viewB) and two buttons that should switch between the two views
Then make one button execute _regionManager.RequestNavigate( "Content", "viewA" )
and the other one _regionManager.RequestNavigate( "Content", "viewB" )
. The views should be registered like this _container.RegisterTypeForNavigation<viewA>( "viewA" )
.
You do not want to use RegisterViewWithRegion
here, just navigate to ViewA
when your app is done initializing.