I'm having some trouble gaining a working understanding of how Region Navigation works in Prism. I'm trying to create an MVVM based app that loads a main window, and displays a view generated by a login form. After the login form is submitted, I then want to navigate to a new UserControl
. I'd like to know if this is also possible without using modules, however for the current implementation, it's modular.
With this current code, the menu bar with a button displays, but not the Login
view.
Main Module
App.xaml.cs
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
moduleCatalog.AddModule<LoginModule.ModuleLoginModule>();
}
MainWindow.xaml:
<Window x:Class="PrismMVVM.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
xmlns:local="clr-namespace:PrismMVVM"
mc:Ignorable="d"
Title="PrismMVVM" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="10*"/>
</Grid.RowDefinitions>
<DockPanel Grid.Row="0">
<Button Content="Code is Poetry" HorizontalAlignment="Left" Width="Auto"/>
</DockPanel>
<ContentControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion">
</ContentControl>
</Grid>
</Window>
MainWindowViewModel.cs
namespace PrismMVVM.ViewModels
{
class MainWindowViewModel : BindableBase
{
public IRegionManager _regionManager;
public MainWindowViewModel(IRegionManager regionManager)
{
_regionManager = regionManager;
regionManager.RequestNavigate("LoginRegion", "Login");
}
}
}
Login Module
ModuleLoginModule.cs:
namespace LoginModule
{
public class ModuleLoginModule : IModule
{
public void OnInitialized(IContainerProvider containerProvider)
{
var regionManager = containerProvider.Resolve<IRegionManager>();
regionManager.RegisterViewWithRegion("LoginRegion", typeof(Login));
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<Login>();
}
}
}
Login.xaml:
<UserControl x:Class="LoginModule.Views.Login"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:LoginModule.Views"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid Background="White" prism:RegionManager.RegionName="LoginRegion">
<StackPanel Panel.ZIndex="1" Margin="150">
<TextBox HorizontalAlignment="Center" VerticalAlignment="Center">Text</TextBox>
<PasswordBox HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Button Background="LightBlue" Content="Login" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
<Rectangle Panel.ZIndex="0" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="LightGray" Height="300" Width="400" />
</Grid>
</UserControl>