Prism WPF 绑定 RegionManager.RegionName [英] Prism WPF Binding RegionManager.RegionName

查看:333
本文介绍了Prism WPF 绑定 RegionManager.RegionName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Prism 7 应用程序,它有两个模块,称为 ModuleA 和 ModuleB.在我的主窗口中,如果显示 A",我希望能够显示任一模块 A.如果显示 B",则单击按钮或 ModuleB按钮被点击.我通过以下方式实现了该行为:

I have a Prism 7 application with two modules, called ModuleA and ModuleB. In my main window I would like to be able to show either ModuleA if "Show A" button is clicked or ModuleB if "Show B" button is clicked. I implemented the behaviour the following way:

主窗口.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

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 的构造函数中设置并且在显示 B"时不会改变被点击.RegionManager.RegionName 的绑定是设计不允许的还是我做错了?

The problem is that the RegionManager.RegionName remains "ModuleA" as set in the constructor of the ViewModel and doesn't change when "Show B" is clicked. Is the binding of the RegionManager.RegionName not allowed by design or am I doing it wrong?

这里也是存储库的链接:https://github.com/moisejbraver/ModulesTest

Here's also the link to the repo: https://github.com/moisejbraver/ModulesTest

推荐答案

区域是用户界面的结构部分.将区域名称分配给特定控件后,不应重新分配区域名称.

A region is a structural part of your user interface. You should not reassign the region name once it has been assigned to a specific control.

如果您需要在区域内导航,请考虑使用 IRegionNavigationService...

If you need to navigate inside a region, consider using the IRegionNavigationService...

这篇关于Prism WPF 绑定 RegionManager.RegionName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆