0
votes

I have a view let's call it 'NestedView', which defines nested region, due to lack of nested region support in XAML I create a view in UserControl constructor like this:

RegionManager.SetRegionName(RegionControl, "MyRegionName");
RegionManager.SetRegionManager(RegionControl, _globalRegionManager);

'NestedView' is shown (added) to some region let's call it 'MainRegion', at some time I need to close this view (remove it from 'MainRegion'). But If I simply remove 'NestedView' from 'MainRegion', the region it has registered 'MyRegionName' will remain registered, and the next time I will try to open 'NestedView' it will throw exception that region 'MeregionName' already registered.

So I need to make sure that when I close view that contain regions, they are unregistered, and all views they contain are disposed. What is the best way to do this?

1

1 Answers

0
votes

I came up with this method in NavigationService:

public interface IRegionContainer
{
    IEnumerable<String> RegionNames { get; }
}

    public void RequestClose(string regionName, string viewContract)
    {
        ContainerRegistration registration = _unityContainer.Registrations.SingleOrDefault(t => t.Name == viewContract);
        if (registration == null) throw new Exception("ViewContract is not registered");
        IEnumerable<object> candidateViews = _regionManager.Regions[regionName].Views.Where(t => t.GetType() == registration.MappedToType);
        foreach (object viewInstance in candidateViews)
        {
            var regionContainer = viewInstance as IRegionContainer;
            if (regionContainer != null) //View defines regions?
            {
                foreach (string rName in regionContainer.RegionNames)
                {
                    var success = _regionManager.Regions.Remove(rName);
                    if (success == false) throw new Exception("Can't remove region: " + rName);
                }
            }
            _regionManager.Regions[regionName].Remove(viewInstance);
        }
    }

Also, PRISM 4.1 is out, and they claim to fix this problem.