Prism WPF Binding RegionManager.RegionName

Sep 28 2020

Tengo una aplicación Prism 7 con dos módulos, llamados ModuleA y ModuleB. En mi ventana principal me gustaría poder mostrar ModuleA si se hace clic en el botón "Mostrar A" o ModuleB si se hace clic en el botón "Mostrar B". Implementé el comportamiento de la siguiente manera:

MainWindow.xaml

<Window x:Class="ModulesTest.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:prism="http://prismlibrary.com/"
    prism:ViewModelLocator.AutoWireViewModel="True"
    Title="{Binding Title}" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <ContentControl prism:RegionManager.RegionName="{Binding Module}" />
    <StackPanel Grid.Row="1">
        <Button Command="{Binding ShowModuleCommand}"
                CommandParameter="A">
            Show A
        </Button>
        <Button Command="{Binding ShowModuleCommand}"
                CommandParameter="B">
            Show B
        </Button>
    </StackPanel>
    
</Grid>

MainWindowViewModel.cs

public class MainWindowViewModel : BindableBase
{
    private string _title = "Prism Application";
    public string Title
    {
        get { return _title; }
        set { SetProperty(ref _title, value); }
    }

    private string fieldName;
    public string Module
    {
        get { return fieldName; }
        set { SetProperty(ref fieldName, value); }
    }

    private DelegateCommand<string> _showModuleCommand;
    public DelegateCommand<string> ShowModuleCommand =>
        _showModuleCommand ?? (_showModuleCommand = new DelegateCommand<string>(ExecuteShowModuleCommand));

    void ExecuteShowModuleCommand(string module)
    {
        Module = "Module" + module;
    }

    public MainWindowViewModel()
    {
        Module = "ModuleA";
    }
}

El problema es que RegionManager.RegionName sigue siendo "ModuleA" según lo establecido en el constructor de ViewModel y no cambia cuando se hace clic en "Mostrar B". ¿El enlace del RegionManager.RegionName no está permitido por diseño o lo estoy haciendo mal?

Aquí también está el enlace al repositorio: https://github.com/moisejbraver/ModulesTest

Respuestas

1 Ehssan Sep 28 2020 at 20:51

Una región es una parte estructural de su interfaz de usuario. No debe reasignar el nombre de la región una vez que se haya asignado a un control específico.

Si necesita navegar dentro de una región, considere usar el IRegionNavigationService...