首页 > 解决方案 > 如何在 Prism 中为我的对话窗口设置区域管理器?

问题描述

我在不使用 Shell 的情况下编写我的应用程序。所以我使用 IDialogService 创建了自己的 Window 并在我的一个模块中打开。就我而言,区域管理器附加到壳牌,但由于我没有它,当我尝试从一个视图导航到另一个视图时,区域管理器不起作用。

我知道 Region Navigation 在 shell 上运行良好(我对其进行了测试),当我用 IDialogService 替换 shell 时,相同的代码停止工作。

这是我所拥有的

<Window x:Class="TechDocs.Views.MainSettingsWindowView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        mc:Ignorable="d"
        Title="MainSettingsWindow" Height="400" Width="750">
    <Grid>
    </Grid>
</Window>

第一个区域的内容。当我单击按钮时,它应该导航到第二个区域。

<UserControl x:Class="TechDocs.Views.SettingsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutoWireViewModel="True"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Button Command="{Binding NodeSelectedCommand}" Name="Button"/>
        <ContentControl prism:RegionManager.RegionName="region"/>
    </Grid>
</UserControl>

在模块中,我将根窗口与保存第二个区域的按钮和内容控件的 UserControl 连接起来。

public class SettingsModule : IModule
{
    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        var dialogService = _containerProvider.Resolve<IDialogService>();
        containerRegistry.RegisterDialog<MainSettingsWindow>("MyWindow");
        containerRegistry.RegisterDialog<SettingsView>("customView");
        containerRegistry.RegisterForNavigation<MyView>();
        dialogService.Show("customView");
    }
}

当我单击按钮时,我会进入此代码

  public void SelectedNode()
        {         
           regionManager.RequestNavigate("region", "MyView");
        }

RequestNavigate 没有给出任何异常,但屏幕上仍然没有出现任何异常。你能解释一下我应该如何在我的窗口中注册区域管理器吗?

标签: wpfnavigationprismregion

解决方案


尝试使用以下静态方法在自定义窗口中显式初始化区域管理器:

RegionManager.SetRegionName(cc, "region");
RegionManager.SetRegionManager(cc, regionManager);

XAML:

<ContentControl x:Name="cc" prism:RegionManager.RegionName="region"/>

推荐阅读