I have two regions. A navigation region and a main region.
My navigation region contains two buttons which call the RequestNavigate method.
The first button loads a view without any parameters
this.tRegionManager.RequestNavigate(RegionNames.MainRegion, ViewNames.VInfoMainViewUri);
The second button should load the same view with some parameters
this.tRegionManager.RequestNavigate(RegionNames.MainRegion, new Uri(ViewNames.VInfoMainViewUri.OriginalString + "" + query.ToString(), UriKind.Relative));
This works fine if no view is loaded. If any view is loaded, a click on any button causes nothing.
I tried to remove every active view from my region, but this causes an error
IViewsCollection col = tRegionManager.Regions[args.RegionName].Views;
foreach (var obj in col)
{
tRegionManager.Regions[args.RegionName].Remove(obj);
}
The region does not contain the specified view.
Parameter name: view
How can I fix this probem?
If you want to create a new view even when there is already an existing view of the same type in the region, you need to implement the INavigationAware
interface either in your View or your ViewModel (Prism will check first the view, and if it doesn't implement INavigationAware
it will also check the ViewModel).
You are interested specifically in the IsNavigationTarget
method, which tells Prism if the current instance of the View should be reused, or if another instance should be created to satisfy the navigation request. So, to always create a new View you would do:
public class MyViewModel : INavigationAware {
bool INavigationAware.IsNavigationTarget(NavigationContext navigationContext)
{
return false;
}
void INavigationAware.OnNavigatedFrom(NavigationContext navigationContext)
{
}
void INavigationAware.OnNavigatedTo(NavigationContext navigationContext)
{
}
}
All of this is explained in greater detail in Chapter 8 of the Prism 4 documentation; they also have an illustration of how it works.