INaviagtionAware issue when DataContext of View is itself

Topics: Prism v4 - Silverlight 4
Nov 24, 2011 at 6:01 AM
Edited Nov 24, 2011 at 6:01 AM

Hi,

I cam across this strange problem where my View is implementing INavigationAware and the dataContext for the view is itself. What is happening is OnNavigatedTo is getting called twice.

[Export("MyView")]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class MyView : INavigationAware
{
     public MyView()
     {
           InitializeComponent();
           DataContext = this;
     }

public void OnNavigatedTo(NavigationContext navigationContext)
        {
        }

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

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }
}

 

Now when I navigate to this view

_regionManager.RequestNavigate("MainRegion", new Uri("MyView", UrikKind.Relative));

It goes into OnNavigatedTo twice. The moment I comment out DataContext = this, it is working as expected. I think this is a bug. I never saw anything like this in the samples as well, where the View is its own DataContext. Can this be verified?

Thanks!

Nov 24, 2011 at 6:44 AM

Actually I resolved this. What you simply need to do is set the DataContext of the LayoutRoot (or whatever your topmost element is in the UserControl) to this instead of setting the DataContext. This works just fine.

Cheers!

Aug 30, 2012 at 5:38 PM
Edited Aug 30, 2012 at 5:38 PM

Hi gan_s! just checking if u happened to find a better solution of this by now as I am also running into same problem.

Aug 31, 2012 at 5:41 AM

Hi imadulhaque,

I would suggest you to have a viewmodel for the view and that should implement the INavigationAware. That will solver your problem or use the solution I used.

Cheers!

Ganesh