INavigationAware in ViewModel not working.

Topics: Prism v4 - WPF 4
Aug 15, 2013 at 8:02 PM
In my LoginViewModel, which is in a PopupRegion, I implemented the INavigationAware, but it is not being called:
public void OnNavigatedTo(NavigationContext navigationContext)
        {
            var parameter = navigationContext.Parameters["cancelOperation"];
            if (parameter != null)
                cancelOperation = (LoginCancelOperation)Enum.Parse(typeof(LoginCancelOperation), navigationContext.Parameters["cancelOperation"]);
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }
The only moment when the OnNavigatedTo is called is when I run the following code:
var region = regionManager.Regions[RegionNames.PopupRegion];
            region.Remove(region.ActiveViews.FirstOrDefault());
That is, only when I remove the LoginView from the PopupRegion.
Aug 15, 2013 at 9:20 PM
Let be more clear and provide more detail: when my app starts, it shows a control in the MainContentRegion and a login control in the PopupRegion. The LoginView has LoginViewModel which implements INavigationAware.
So, here is a snippet of the module:
class MainModule : IModule

registrationManager.RegisterViewWithNavigation<TransmissionView>(MainNavigationCommands.ShowTransmissionListDialog, RegionNames.PopupRegion);

regionManager.RegisterViewWithRegion(RegionNames.MainContentRegion, typeof(OperationView));
            registrationManager.RegisterViewWithNavigation<OperationView>(MainNavigationCommands.NavigateToVideoMixer, RegionNames.MainContentRegion);
            regionManager.RequestNavigate(RegionNames.MainContentRegion, typeof(OperationView).Name);

registrationManager.RegisterViewWithNavigation<LoginView>(MainNavigationCommands.ShowLoginDialog, RegionNames.PopupRegion);
            regionManager.RequestNavigate(RegionNames.PopupRegion, typeof(LoginView).Name);
As you can see, in the Initialize method of the MainModule, I already call RequestNavigation on the LoginView. The OnNavigatedTo is not called.
Ok, moving on...
When user click in Login button on the LoginView, I call
regionManager.RequestNavigate(RegionNames.PopupRegion, ViewNames.Transmission);
In the TransmissionView, when user click Cancel button, I have to close the view, to do that I remove the only view in the PopupRegion:
var region = regionManager.Regions[RegionNames.PopupRegion];
            region.Remove(region.ActiveViews.FirstOrDefault());
When I do that, the OnNavigatedTo method is called in the LoginViewModel. How's that possible?
Developer
Aug 15, 2013 at 9:46 PM
Hi,

I tried using the RegionPopupBehavior of the Stock Trader RI as out of the box to show a simple popup and didn't experience this behavior; the OnNavigatedTo method executed correctly when the view was shown.

It is possible that the PopupRegionBehavior or the WindowWrapper.Desktop you are using in your application was modified to show the Window using the ShowDialog method instead of Show. This would show the Window as a dialog, but would also freeze any logic being executed until the popup Window is closed, included Prism's navigation logic as well as other region behaviors.

Regards,

Damian Cherubini
http://blogs.southworks.net/dcherubini
Aug 15, 2013 at 9:52 PM
That's exactly what's happening. If I call Show instead of ShowDialog, it works.
But I want to block the user from having access to the parent Window.
Developer
Aug 16, 2013 at 6:42 PM
Hi,

A possible approach could be to disable the parent window manually when showing the window, and re-enabling it once the popup is closed. For example, in the code behind of the Shell window, you could subscribe to an event using the EventAggregator. When this event is raised you can change the IsEnabled property accordingly to disable / enable the main window.

Then, you could modify the DialogActivationBehavior to raise the event in the PrepareContentDialog and CloseContentDialog methods in order to block / unblock the parent window.

I have created a small sample portraying this approach. You can find it in the following SkyDrive folder as "PopupRegionAndNavigation:"
I hope this helps,

Damian Cherubini
http://blogs.southworks.net/dcherubini
Aug 16, 2013 at 7:16 PM
Edited Aug 16, 2013 at 7:16 PM
It is a workaround, but not as the ShowDialog(), which does not enable any interaction with the parent window at all.
For now, it will do it...

Thanks!
Oct 27, 2013 at 5:59 AM
Edited Oct 27, 2013 at 6:00 AM
Hi

You can fix this issue by change code in Prism.
Find RegionNavigationService.cs in Prism.Desktop and then goto to ExecuteNavigation then find InvokeOnNavigationAwareElement(view, (n) => n.OnNavigatedTo(navigationContext));. If you call this function before this.Region.Activate(view); it will works

The Best