Allows to create scoped regions
Register DelayedScopedRegionCreationBehavior in Container
...ServiceLocator will return this behavior for DelayedRegionCreationBehavior
Register ShellService if we want to show multiple shells
protected override void RegisterRequiredTypes(IContainerRegistry containerRegistry)
{
// DelayedScopedRegionCreationBehavior
containerRegistry.Register<DelayedRegionCreationBehavior, DelayedScopedRegionCreationBehavior>();
// Shell Service
containerRegistry.RegisterSingleton<IShellService, ShellService>();
base.RegisterRequiredTypes(containerRegistry);
}
Add RegionManagerAwareBehavior (RegionBehavior) to resolve IRegionManagerAware for Views
protected override void ConfigureDefaultRegionBehaviors(IRegionBehaviorFactory regionBehaviors)
{
base.ConfigureDefaultRegionBehaviors(regionBehaviors);
regionBehaviors.AddIfMissing(RegionManagerAwareBehavior.BehaviorKey, typeof(RegionManagerAwareBehavior));
}
Inject the service and use it to show new shell
public class MainWindowViewModel
{
private IShellService shellService;
private DelegateCommand showShellCommand;
public MainWindowViewModel(IShellService shellService)
{
if (shellService is null)
throw new ArgumentNullException(nameof(shellService));
this.shellService = shellService;
}
public DelegateCommand ShowShellCommand
{
get
{
if (showShellCommand == null)
showShellCommand = new DelegateCommand(ExecuteShowShellCommand);
return showShellCommand;
}
}
private void ExecuteShowShellCommand()
{
shellService.ShowShell<MainWindow>();
}
}
To get the RegionManager for the region scope
public class MainWindowViewModel : IRegionManagerAware
{
public IRegionManager RegionManager { get; set; }
}
Resources: