WPF - Prism 7.1 - 导航 - 掌握选项卡控件 - Modal/Dialog Window

WPF - Prism 7.1 - Navigation - Mastering Tab Control - Modal/Dialog Window

我正在使用 Prism 7.1 导航框架 (WPF) 获取一个对话框 window 使用下面的配置弹出。这是成功的。但是,我希望此弹出窗口具有可以在其中来回导航的选项卡。当我单击弹出框上的按钮以尝试在其中显示 ViewA 时,没有任何反应。通过设置断点,我看到导航路径被命中,并显示正确的视图名称。参考PopUpWindow.cs。但是,当它去解析视图时,视图不显示。更糟糕的是,没有抛出任何错误!我很困惑为什么会这样。

假设我的命名空间是正确的,我做错了什么?

PrismApplication.cs

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    containerRegistry.RegisterForNavigation<ViewA>();
}

//Have tried register type, register type for navigation, etc etc.

MainWindowViewModel.xaml

<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Height="350" Width="525">
    <i:Interaction.Triggers>
        <prism:InteractionRequestTrigger SourceObject="{Binding NotificationRequest}">
            <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True" />
        </prism:InteractionRequestTrigger>
    </i:Interaction.Triggers>
    <StackPanel>
        <Button Margin="5" Content="Raise Default Notification" Command="{Binding NotificationCommand}" />
    </StackPanel>

MainWindowViewModel.cs

public MainWindowViewModel
{
    public InteractionRequest<INotification> NotificationRequest { get; set; }
    public DelegateCommand NotificationCommand { get; set; }

    public MainWindowViewModel()
    {
        NotificationRequest = new InteractionRequest<INotification>();
        NotificationCommand = new DelegateCommand(RaiseNotification);
    }

    void RaiseNotification()
    {
        NotificationRequest.Raise(new PopupWindow());
    }
}

PopUpWindow.xaml

<UserControl 
        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"
        Height="350" Width="525">
    <DockPanel LastChildFill="True">
        <StackPanel Orientation="Horizontal" DockPanel.Dock="Top" Margin="5" >
            <Button Command="{Binding NavigateCommand}" CommandParameter="ViewA" Margin="5">Navigate to View A</Button> 
        </StackPanel>
        <ContentControl prism:RegionManager.RegionName="ContentRegion" Margin="5"  />
    </DockPanel>
</UserControl>

PopUpWindow.cs

public class PopupWindowViewModel
{
    private readonly IRegionManager _regionManager;

    public DelegateCommand<string> NavigateCommand { get; private set; }

    public PopupWindowViewModel(IRegionManager regionManager)
    {
        _regionManager = regionManager;

        NavigateCommand = new DelegateCommand<string>(Navigate);
    }

    private void Navigate(string navigatePath)
    {
        if (navigatePath != null)
            _regionManager.RequestNavigate("ContentRegion", navigatePath); 

        //During debugging, this correctly shows navigatePath as "ViewA"
    }
}

ViewA.xaml

<UserControl 
             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">
    <Grid>
        <TextBlock Text="ViewA" FontSize="48" HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid>
</UserControl>

InteractionRequest 模式有点古怪。您需要确保所有应该对请求做出反应的视图在可视化树中都有必要的 InteractionRequestTrigger。因此,解决问题的直接方法是将 XAMLMainWindowView.xaml 复制到 ViewA.xaml:

<UserControl 
        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"
        Height="350" Width="525">
   <i:Interaction.Triggers>
        <prism:InteractionRequestTrigger SourceObject="{Binding NotificationRequest}">
            <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True" />
        </prism:InteractionRequestTrigger>
    </i:Interaction.Triggers> 
    <!-- ... -->
</UserControl>

然后确保在 ViewA 的视图模型中添加 NotificationRequest。请注意,您仍然可能会遇到交互请求不起作用的情况。例如。在数据模板 添加触发器时。不过,只要您将它们放在 UserControl 级别就可以了。

对此(有缺陷的)设计的一个可能改进是创建一种行为,您可以在其中以编程方式添加这些交互触发器。

也许只是没有找到您的观点。 第二个参数不应该是 url 而不是字符串吗? 从这里: https://prismlibrary.github.io/docs/wpf/Navigation.html

    IRegionManager regionManager = ...;
regionManager.RequestNavigate("MainRegion",
                                new Uri("InboxView", UriKind.Relative));

检查您的视图在哪里以及路径应该是什么。 我想你可以证明使用类似的东西:

var testinstance = System.Windows.Application.LoadComponent(testUrl);

https://docs.microsoft.com/en-us/dotnet/api/system.windows.application.loadcomponent?view=netframework-4.7.2

如果您使用的是 MEF,我认为您还需要使用导出属性标记视图。

希望你的问题只是你忘记了文件夹或类似的东西。

如果不是,则可能与 regionmanager 没有获取您所在地区的参考有关。

不在可视化树中的区域将被区域管理器忽略。您在 PopUpWindow 中定义了 ContentRegion(这是延迟创建的),因此它不存在,并且忽略了未知区域的导航请求。

详述 and ,在这种情况下,您必须在包含它的视图的构造函数中手动添加该区域:

RegionManager.SetRegionName( theNameOfTheContentControlInsideThePopup, WellKnownRegionNames.DataFeedRegion );
RegionManager.SetRegionManager( theNameOfTheContentControlInsideThePopup, theRegionManagerInstanceFromUnity );

来自 ServiceLocator 的区域经理:

ServiceLocator.Current.GetInstance<IRegionManager>()