Prism WPF Binding RegionManager.RegionName

Sep 28 2020

Eu tenho um aplicativo Prism 7 com dois módulos, chamados de Módulo A e Módulo B. Na minha janela principal, gostaria de poder mostrar o Módulo A se o botão "Mostrar A" for clicado ou o Módulo B se o botão "Mostrar B" for clicado. Implementei o comportamento da seguinte maneira:

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";
    }
}

O problema é que RegionManager.RegionName permanece "ModuleA" conforme definido no construtor do ViewModel e não muda quando "Show B" é clicado. A vinculação de RegionManager.RegionName não é permitida pelo design ou estou fazendo isso errado?

Este também é o link para o repo: https://github.com/moisejbraver/ModulesTest

Respostas

1 Ehssan Sep 28 2020 at 20:51

Uma região é uma parte estrutural da interface do usuário. Você não deve reatribuir o nome da região depois de ter sido atribuído a um controle específico.

Se você precisa navegar dentro de uma região, considere usar o IRegionNavigationService...