ปริซึม WPF Binding RegionManager.RegionName

Sep 28 2020

ฉันมีแอพพลิเคชั่น Prism 7 ที่มีสองโมดูลชื่อ ModuleA และ ModuleB ในหน้าต่างหลักของฉันฉันต้องการให้แสดง ModuleA อย่างใดอย่างหนึ่งหากคลิกปุ่ม "แสดง A" หรือ ModuleB หากคลิกปุ่ม "แสดง B" ฉันใช้พฤติกรรมดังต่อไปนี้:

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

ปัญหาคือ RegionManager.RegionName ยังคงเป็น "ModuleA" ตามที่ตั้งค่าไว้ในตัวสร้างของ ViewModel และจะไม่เปลี่ยนแปลงเมื่อคลิก "Show B" การเชื่อมโยงของ RegionManager.RegionName ไม่ได้รับอนุญาตจากการออกแบบหรือฉันทำผิด?

นี่คือลิงค์ไปยัง repo: https://github.com/moisejbraver/ModulesTest

คำตอบ

1 Ehssan Sep 28 2020 at 20:51

ภูมิภาคเป็นส่วนโครงสร้างของอินเทอร์เฟซผู้ใช้ของคุณ คุณไม่ควรกำหนดชื่อภูมิภาคใหม่เมื่อได้รับมอบหมายให้กับการควบคุมเฉพาะแล้ว

หากคุณต้องการนำทางภายในภูมิภาคให้พิจารณาใช้IRegionNavigationService...