Prism WPF Binding RegionManager.RegionName

Sep 28 2020

Ho un'applicazione Prism 7 con due moduli, chiamati ModuleA e ModuleB. Nella mia finestra principale vorrei essere in grado di mostrare sia ModuleA se si fa clic sul pulsante "Mostra A" o ModuleB se si fa clic sul pulsante "Mostra B". Ho implementato il comportamento nel modo seguente:

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

Il problema è che RegionManager.RegionName rimane "ModuleA" come impostato nel costruttore del ViewModel e non cambia quando si fa clic su "Mostra B". L'associazione di RegionManager.RegionName non è consentita in base alla progettazione o sto sbagliando?

Ecco anche il collegamento al repository: https://github.com/moisejbraver/ModulesTest

Risposte

1 Ehssan Sep 28 2020 at 20:51

Una regione è una parte strutturale della tua interfaccia utente. Non è necessario riassegnare il nome della regione dopo che è stato assegnato a un controllo specifico.

Se hai bisogno di navigare all'interno di una regione, considera l'utilizzo del IRegionNavigationService...