INavigationTarget not being hit

Topics: Prism v4 - Silverlight 4
Jan 25, 2012 at 2:55 PM
Edited Jan 25, 2012 at 3:03 PM

Hi all

I have a viewmodel that implements INavigationAware interface.  In IsNavigationTarget I'm returning false as I would like a new view opened, but what is happening is that the same view is being displayed in the region with previous values.

This is a login screen with 2 properties(username and password).  I'm using MEF to export the viewmodel as follows:

 

[Export(typeof(ILoginViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class LoginViewModel : BaseINPC, ILoginViewModel, INavigationAware
    {
         //public username and password properties
         //with property changed


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

    }

My loginview.cs is as follows:
[Export("LoginView")]
    public partial class LoginView : UserControl
    {
        [Import]
        public ILoginViewModel ViewModel
        {
            set { this.DataContext = value; }
        }

        public LoginView()
        {
            InitializeComponent();
        }
    }
When loading the loginview I use the following:But the problem arises 
when the user logs out, I want to show the loginview so I requestnavigate to the view:
_regionManager.RequestNavigate("MainRegion", "LoginView");

The view loads but the values for username and password are still the values from the 
first time the loginview was loaded so I'm not getting a new view/viewmodel.
I put a breakpoint in the IsNavigationTarget but it's not been hit.
This must be something simple, looked at other posts regarding contract names etc 
but to no avail, any help much appreciated.
Thanks
Paul
ps tried to fix formatting but no luck.
Developer
Jan 25, 2012 at 3:35 PM

Hi,

I believe that your problem could be related to the fact that your LoginView is exported as a singleton (as this is the default behavior in MEF). Therefore, each time you perform navigation the container returns the same instance of this view.

Based on this, you could try including [PartCreationPolicy(CreationPolicy.NonShared)] attribute also in the LoginView view.

I hope you find this helpful,

Agustin Adami
http://blogs.southworks.net/aadami


Jan 25, 2012 at 4:13 PM

Agustin to the rescue yet again :)

Thanks that was it, new it was something simple but couldn't see the wood from the trees.

Paul