The application that I am currently writing uses a Tab Control as its primary menu structure. Tabs allow caching of views; the tab retains the loaded view when another tab is selected.
Each tab contains a Region placeholder:
<TabControl>
<TabItem Name="Home">
<ContentControl prism:RegionManager.RegionName="HomeRegion"/>
</TabItem>
<TabItem Name="Project" Header="Program">
<ContentControl prism:RegionManager.RegionName="ProjectRegion"/>
</TabItem>
<TabItem Name="TestPlan" Header="Test Plan">
<ContentControl prism:RegionManager.RegionName="TestPlanRegion"/>
</TabItem>
</TabControl>
PRISM provides for navigation to Tab Items via the INavigationAware
interface, automagically creating the View in a new tab if it doesn't already exist, or refreshing an existing View in an existing tab.
public class PersonDetailViewModel : INavigationAware
{
// Data-Bound property
public Person SelectedPerson { get; set; }
// INavigationAware method
public void OnNavigatedTo(NavigationContext navigationContext)
{
var person = navigationContext.Parameters["person"] as Person;
if (person != null)
SelectedPerson = person;
}
}
The navigation is accomplished thusly:
var parameters = new NavigationParameters();
parameters.Add("person", person);
if (person != null)
_regionManager.RequestNavigate("PersonDetailsRegion", "PersonDetail", parameters);
Here's the problem: clicking a tab does not accomplish the usual navigation. It merely displays the existing view, which is already loaded on startup. So there's no opportunity for refreshing the tab when the tab is selected.
The behavior I want: I'd like the tab to load its content on first use, whether that's via a RequestNavigate
call or clicking the tab, and then re-use its content on subsequent uses, including the passing of parameters like Person
.
Unfortunately, at the moment, I think I'm left with two less than ideal choices:
Simulate a
RequestNavigate
when a user clicks on the tab, presumably by hooking theSelectionChanged
event on the Tab Control. For this to work, I need to lazy load the View into the tab, since applying a RequestNavigate on a tab click will make the tab load twice on first use, defeating any caching benefit I might have, orUse conventional menus that
RequestNavigate
to a single Region instead of tabs, which has the virtue of simplicity, but which defeats the caching benefits altogether.
Any thoughts on how I might make this work?
clicking a tab does not accomplish the usual navigation. It merely displays the existing view, which is already loaded on startup
sounds like it is exactly the caching of views you wanted in the first place? – Haukinger