Skip to content

Instantly share code, notes, and snippets.

--- implementation
public class CustomBehavior : IRegionBehavior
{
...
}
--- bootstrapper
protected override IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
{
namespace Prism.Regions
{
/// <summary>
/// Interface for allowing extensible behavior on regions.
/// </summary>
public interface IRegionBehavior
{
/// <summary>
/// The region that this behavior is extending.
/// </summary>
public IRegion Initialize(T regionTarget, string regionName)
{
if (regionName == null)
throw new ArgumentNullException(nameof(regionName));
IRegion region = this.CreateRegion();
region.Name = regionName;
SetObservableRegionOnHostingControl(region, regionTarget);
// creating duplicate regions via region.add correctly throws exception
var region = regionManager.Regions["ViewA"];
region.Add(unityContainer.Resolve<ViewA>());
region.Add(unityContainer.Resolve<ViewA>());
// creating duplicate regions via regionManager.AddToRegion does not throw exception
regionManager.AddToRegion("ViewA", unityContainer.Resolve<ViewA>());
regionManager.AddToRegion("ViewA", unityContainer.Resolve<ViewA>());
// Uncomment the following three lines and we get exception, this gets fixed with scoped region
var region = regionManager.Regions["ViewA"];
region.Add(unityContainer.Resolve<ViewA>());
region.Add(unityContainer.Resolve<ViewA>());
// the following works
// regionManager.AddToRegion("ViewA", unityContainer.Resolve<ViewA>());
// regionManager.AddToRegion("ViewA", unityContainer.Resolve<ViewA>());
public IRegionManager AddToRegion(string regionName, object view)
{
if (!Regions.ContainsRegionWithName(regionName))
throw new ArgumentException(string.Format(Thread.CurrentThread.CurrentCulture, Resources.RegionNotFound, regionName), nameof(regionName));
return Regions[regionName].Add(view);
}
--ViewA.xaml
<ItemsControl regions:RegionManager.RegionName="RegionA"></ContentControl>
--ViewB.xaml
<ContentControl regions:RegionManager.RegionName="RegionB"></ItemsControl>
--ViewA.xaml
<ItemsControl regions:RegionManager.RegionName="RegionA"></ContentControl>
--ViewB.xaml
<ContentControl regions:RegionManager.RegionName="RegionB"></ItemsControl>
--ViewC.xaml
<ContentControl regions:RegionManager.RegionName="RegionC"></ContentControl>
--ViewA.xaml
<ItemsControl regions:RegionManager.RegionName="RegionA"></ContentControl>
--ViewB.xaml
<ItemsControl regions:RegionManager.RegionName="RegionB"></ItemsControl>
--ViewC.xaml
<ContentControl regions:RegionManager.RegionName="RegionC"></ContentControl>
--ViewA.xaml
<ContentControl regions:RegionManager.RegionName="RegionA"></ContentControl>
--ViewB.xaml
<ItemsControl regions:RegionManager.RegionName="RegionB"></ItemsControl>
--ViewC.xaml
<ContentControl regions:RegionManager.RegionName="RegionC"></ContentControl>