In my current project I am using the MVVMLight framework.
I am experiencing some difficulties with the navigation between views. Currently, I am using DataTemplates to navigate between Views. However, every time one navigates to another view, this view is recreated. Also, I am using nested views. In all, I feel that I need a more advanced framework to manage view-based navigation.
I was thinking to use Prism view-based navigation. I would like to define Regions with the Prism RegionManager; Register my views to regions; and subsequently execute navigation requests.
How can I integrate the Prism RegionManager with the MVVMLight framework?
Ultimately, I want to be able to do the following:
public class NavigationViewModel : ViewModelBase
{
private readonly IRegionManager _regionManager;
// Constructor
public NavigationViewModel(IRegionManager regionManager)
{
_regionManager = regionManager;
}
...
// Navigate
private void ExecuteNavigationCommand(string ViewName)
{
_regionManager.RequestNavigate(RegionNames.ContentRegion, ViewName);
}
}
Any ideas on how to achieve this?
Edit:
I have created a NavigationService that implements the following interface:
public interface INavigationService
{
RelayCommand<string> NavigateCommand { get; }
List<String> History { get; }
Dictionary<String, UserControl> Views { get; }
void AddView(UserControl view, RegionName region);
void RemoveView(UserControl view);
void ResetView(UserControl view);
UserControl GetView(Type viewType);
void NavigateRequest(Type viewType);
}
This NavigationService is injected to my ShellViewModel via MVVMLight's SimpleIoC. In the ShellViewModel I register my navigatable views as follows:
// Add navigatable view instances and corresponding region
_navigationService.AddView(new HomeView(), RegionName.MainRegion);
_navigationService.AddView(new QuickscanView(), RegionName.MainRegion);
_navigationService.AddView(new PartsView(), RegionName.MainRegion);
_navigationService.AddView(new NavigationView(), RegionName.NavigationRegion);
_navigationService.AddView(new StatusbarView(), RegionName.StatusRegion);
Subsequently, I can set views to my predefined regions as follows:
// Set Navigation and Statusbar
this.NavigationRegion = _navigationService.GetView(typeof(NavigationView));
this.StatusRegion = _navigationService.GetView(typeof(StatusbarView));
Also, I can navigate content in my MainContent as follows:
// Navigate to HomeView in MainRegion
_navigationService.NavigateRequest(typeof(HomeView));
The NavigationService is binded to the menu buttons via a public property of INavigationService as follows:
<Button Text="Results"
Command="{Binding NavigationService.NavigateCommand}"
CommandParameter="{Binding ResultsNavigateParameter}"/>