Discovering dynamically created regions

Sep 2, 2008 at 5:44 PM
I am trying to programmatically create a view and its window to display on screen. It looks something like the following code below.In the code the module is already loaded, a request to display this view was invoked and captured and now to display the window:


// Module

Window GetModuleShell()
{
  Window window = container.Resolve<Window>("WindowFolder");
  return window;
}

void DisplayPatientFolder()
{
  Window window = GetModuleShell();
  window.SetValue(RegionManager.RegionNameProperty, "FolderArea");
  // Regionalize FolderView
  FolderView myFolder = container.Resolve<FolderView>();
  //Fails on the next line because the region is not discovered
  IRegion folderRegion = container.Resolve<IRegionManager>().Regions["FolderArea"]; 
  folderRegion.Add(myFolder);
  folderRegion.Activate(myFolder);
  // Regionalize ContentView
  ContentView myContent = container.Resolve<ContentView>();
  IRegion contentRegion = container.Resolve<IRegionManager>().Regions["ContentArea"];
  contentRegion.Add(myContent);
  contentRegion.Activate(myContent);
  // Dispose views
  window.Closing += 
     delegate 
      { 
       container.Teardown(myContent);
       container.Teardown(myFolder);
      };
  window.Display();
}

I tried container.BuildUp(window) after i assigned the RegionName property but the Region is still not discovered. Can anyone spread some love on this issue?

Thanks in advance,

Andres Olivares

Sep 2, 2008 at 6:09 PM
I have updated the following code:

window.SetValue(RegionManager.RegionNameProperty, "FolderArea");
...
  window.Closing += 
     delegate 
      { 
       container.Teardown(myContent);
       container.Teardown(myFolder);
      };

to 

regionManager.AttachNewRegion(window, "FolderArea");
...
      window.Closing +=
        delegate
          {
            folderRegion.Deactivate(myFolder);
            folderRegion.Remove(myFolder);
            regionManager.Regions.Remove("FolderArea");
            container.Teardown(patientContent);
            container.Teardown(patientFolder);
          };

This works fine. RegionManager should be, in my opiniion, a little smarter with it's regions. A developer should not have to do so much clean up in this aspect.

Andres.

Sep 2, 2008 at 6:25 PM
Hi Andrew,  I find the Unit Test to be a great starting point when trying to code dynamically (or simply to learn).

CompositeWPF\Source\CAL\CompositeApplicationLibrary.sln

RegionManagerFixture.cs has the following test:


       [TestMethod]
        public void CanAddRegion()
        {
            IRegion region1 = new MockRegion();

            RegionManager regionManager = new RegionManager();
            regionManager.Regions.Add("MainRegion", region1);

            IRegion region2 = regionManager.Regions["MainRegion"];
            Assert.AreSame(region1, region2);
        }


If you'll examine Region.cs you'll find a lot of wiring-up occurs during .Add().  If this doesn't help you I trust your answer lies in one of the many test fixtures associated with regions.