wpf – prism – 使用RequestNavigate时不调用IsNavigationTarget

我正在尝试学习棱镜导航支持.目前,我有一个prism Region,我想使用RegionManager.RequestNavigate()将视图加载到该区域.导航确实发生,但是,即使导航目标视图的ViewModel实现了INavigationAware接口,也不会调用INavigationAware的IsNavigationTarget().这是我正在使用的代码.

贝壳:

<StackPanel Margin="10">
    <TextBlock Text="Main Window"/>
    <Button Content="RegionA" Command="{Binding NavigateToACommand}" />
    <ContentControl prism:RegionManager.RegionName="MainRegion"/>
</StackPanel>

ShellViewModel:

private void NavigateToA () {
    Uri uri = new Uri("RegionAView", UriKind.Relative);
    RegionManager.RequestNavigate("MainRegion", uri);
}

RegionAView:

<UserControl x:Class="NavigationExample.RegionAView"
    <Grid>
        <TextBlock Text="This is Region A"/>
    </Grid>
</UserControl>

RegionAViewModel

public class RegionAViewModel : INavigationAware{

    public RegionAViewModel() {
    }

    public bool IsNavigationTarget(NavigationContext navigationContext) {
        return false; //Not Invoked
    }

    public void OnNavigatedTo(NavigationContext navigationContext) {
        //Gets Invoked
    }
}

RegionAView.xaml.cs

[Export("RegionAView")]
public partial class RegionAView : UserControl {
    public RegionAView() {
        InitializeComponent();
    }
}

为什么在完成导航之前不会调用IsNavigationTarget()?

最佳答案 我认为您的问题是您将视图导出为单例.修改VM和V如下:

[Export("RegionAView")] 
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class RegionAView : UserControl 
{     
public RegionAView() 
{         
InitializeComponent();     
} 
} 

基本上,当您有现有实例时,将调用IsNavigationTarget.但它不适用于新创建的实例.

点赞