Prism - IsNavigationTarget not called when using RequestNavigate

I am trying to learn Prism navigation support. I currently have a prism region and I want to load a view into this region using RegionManager.RequestNavigate (). Navigation occurs, however, IsNavigationTarget () from INavigationAware is not called even if the ViewModel implements the INavigationAware interface in the navigation view. Here is the code I'm using.

Shell:

<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(); } } 

Why isnavigationTarget () not starting until navigation is complete?

+4
source share
1 answer

I think your problem is that you are exporting your view as singleton. change VM and V as follows:

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

Basically, an IsNavigationTarget will be called when you have existing instances. But it will not work for the newly created instance.

+2
source

Source: https://habr.com/ru/post/1403010/


All Articles