views:

374

answers:

2

I have a Region which can have only one active view at a time. I want to add new view to the region on user action and remove the existing view from the same region. I also want to maintain the cache of few views. If the no of view is more than the specified limit then I will remove the oldest view. Is there any direct support for it or I have to implement the Region adapter for it. Is there any other better approach for the same?

+1  A: 

Well, let me answer your two questions.

First, if you want a Region to only show one view (like say you have a region defined as a ContentControl), that is possible. You can add many views to that region and only the active one will be shown. To show a different view in that region that has already been added, you would simply Activate that view:

var region = regionManager.Regions["TabRegion"];

region.Add(view1);
region.Add(view2);

region.Activate(view2);

In this way, you can have many instantiated views ready to go, but only one visible.

Second, with the expirations. I'd say a region adapter would be the cleanest and most correct way, but you could just create an expiring cache for these and when they expire, you can remove them from the region if they aren't active:

var region = regionManager.Regions["TabRegion"];

region.Add(view1);
regionTracker.Add(view1, region, TimeSpan.FromMinutes(10));
region.Add(view2);
regionTracker.Add(view2, region, TimeSpan.FromMinutes(10));

region.Activate(view2);

And then the implementation of your expiration for your regionTracker could just:

if(!region.ActiveViews.Contains(ViewThatJustExpired))
{
     region.Remove(ViewThatJustExpired);
}

It's a bit half-baked, but hopefully this will give you some idea of where to go.

Anderson Imes
A: 

Take a look at my blog post about dynamic module loading in PRISM with Navigation. In that post you'll see how I use a multiple-view container, then swap views into and out of focus. It involves having an interface for navigation, then raising events that swap the view state using the visual state manager.

Click Here to View

Jeremy

Jeremy Likness