Home > Software design >  How can I set region Manager for my dialog window in Prism?
How can I set region Manager for my dialog window in Prism?

Time:10-25

I write my app without using Shell. So I created my own Window using IDialogService and opened in one of my modules. As far, as I am concerned, Region Manager is attached to Shell, but due to the fact I don't have it, region manager doesn't work when I try to navigate from one view to another.

I know that Region Navigation works fine with the shell (I tested it) and the same code stops working when I substitute the shell with IDialogService.

Here is what I have

<Window x:Class="TechDocs.Views.MainSettingsWindowView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        mc:Ignorable="d"
        Title="MainSettingsWindow" Height="400" Width="750">
    <Grid>
    </Grid>
</Window>

Content for the first region. When I click the button, it should navigate to the second region.

<UserControl x:Class="TechDocs.Views.SettingsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutoWireViewModel="True"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Button Command="{Binding NodeSelectedCommand}" Name="Button"/>
        <ContentControl prism:RegionManager.RegionName="region"/>
    </Grid>
</UserControl>

In module I connect my root window with UserControl which holds the button and Content control for the second region.

public class SettingsModule : IModule
{
    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        var dialogService = _containerProvider.Resolve<IDialogService>();
        containerRegistry.RegisterDialog<MainSettingsWindow>("MyWindow");
        containerRegistry.RegisterDialog<SettingsView>("customView");
        containerRegistry.RegisterForNavigation<MyView>();
        dialogService.Show("customView");
    }
}

And when I click the button I get in this code

  public void SelectedNode()
        {         
           regionManager.RequestNavigate("region", "MyView");
        }

RequestNavigate doesn't give any exceptions, but still nothing appears on the screen. Could you please explain how I should register region manager with my window?

CodePudding user response:

Try to initialize the region manager explicitly in your custom window using the static methods below:

RegionManager.SetRegionName(cc, "region");
RegionManager.SetRegionManager(cc, regionManager);

XAML:

<ContentControl x:Name="cc" prism:RegionManager.RegionName="region"/>
  • Related