IActiveAware and INavigationAware

Topics: Prism v4 - WPF 4
Apr 27, 2011 at 9:51 AM

Was just wondering why INavigationAware can be attached to view models, but IActiveAware cannot (i.e. IActiveAware only seems to work if attached to the view).

Any ideas why this is or should I be able to attach IActiveAware to views?

Thanks Liam.

Apr 27, 2011 at 3:01 PM

Hi Liam,

The RegionActiveAwareBehavior behavior, which can be found in the Regions/Behaviors folder in the Prism.Silverlight project, monitors the region's ActiveViews collection to see if a view or view model is added or removed (by subscribing to its CollectionChanged event), and modifies the IsActive property of the View and/or ViewModel accordingly.

From the Prism MSDN documentation, “You can implement the IActiveAware interface on child views or view models. It is primarily used to track the active state of a child view within a region (…)”

To illustrate the aforementioned, here’s the code from the RegionActiveAwareBehavior .InvokeOnActiveAwareElement method, which updates the IsActive property of both the View and the ViewModel (the invocation action that is passed as a parameter updates the IsActive property):

private static void InvokeOnActiveAwareElement(object item, Action<IActiveAware> invocation)
       {
           var activeAware = item as IActiveAware;
           if (activeAware != null)
           {
               invocation(activeAware);
           }
 
           var frameworkElement = item as FrameworkElement;
           if (frameworkElement != null)
           {
               var activeAwareDataContext = frameworkElement.DataContext as IActiveAware;
               if (activeAwareDataContext != null)
               {
                   invocation(activeAwareDataContext);
               }
           }
}

I hope you find this information useful.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

Apr 28, 2011 at 11:46 AM

Hi Miguel

thanks for the response. You are correct - both IActiveAware and iNavigationAware can be implemented in either the view or the viewmodel. This is good - means I can move more code out of the view and into the viewmodel.

Liam