Sample prism app using IActiveAware?

Topics: Prism v4 - WPF 4
Oct 27, 2011 at 6:25 PM

I'm attempting to implement IActiveAware in a prism based project in order to have a well designed means of finding out which is the active view at any given point in time.  I've looked through all of the quickstarts that come with the Prism code, I've checked the PrismTrainingKit (http://prismtk.codeplex.com/), I've searched through this discussion board and finally off into the web at large, and am amazed to have yet to find an example of someone actually implementing IActiveAware.

The most complete explanation I've found thus far is on Brian Noyes' blog:  http://www.softinsight.com/bnoyes/2009/12/08/DetectingTheActiveViewInAPrismApp.aspx  --- And this is what I've tried to implement in my own project, however, the IsActive property is definitely not changing as views are brought in and out of sight.

 

My first question is:  Why does it seem to be such an unused feature?  Are there issues?  Does it not work as well as it should, or is it just extremely difficult to implement?  Definitely the biggest surprise was that none of the sample code that comes with prism seem to use IActiveAware.

My second, and much more important question is:  Does anyone know of a sample, and hopefully an accompanying tutorial for using IActiveAware to detect active views of a prism based application?  I think I might be able to piece things together, if I could see it working properly.

 

If anyone can point me in the right direction on this, I would be extremely grateful... Thank you so much for taking the time to read my questions, and thanks in advance for your insight.    =)

 

Sincerely,

Jon B

Developer
Oct 28, 2011 at 5:15 PM

Hi,

We prepared a sample application using IActiveAware. In this sample you will find a region defined in a ListBox control, where we added a view four times. The view model for that view implements the IActiveAware interface. The view contains a TextBox, with the IsEnabled property bound to it's view model's IsActive property, so each time you select a view, its IsActive property is set to true (due to the RegionActiveAwareBehavior which is responsible for notifying a view if it is active or inactive); and when the view is unselected, its IsActive property is set to false.

You can find the sample in my skydrive account, under the IActiveAwareListBoxSample name.

I hope you find this helpful.

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


Oct 30, 2011 at 2:03 AM

Hi Agustin,

 

Thanks so much for your quick reply and assistance!  After downloading and examining the sample you posted, I have a follow-up question I was wondering if you (or anyone) might help me understand:

I see that in HelloWorldView.xaml, the IsEnabled property of the text box is bound to the IsActive property of the viewModel (HelloWorldViewModel.cs).  Based on the Prism 4 documentation and Brian Noyes' blog post I mentioned in my original post, I had been under the impression that IActiveAware 's IsActive property did not require any binding in the xaml of the application, as opposed to relying on the text boxes' IsEnabled property.  A couple of things that I worry about are:

  1. If I didn't want the textBoxes to become disabled when the user leaves them, would my only option be to find some other property to bind to?
  2. If a viewModel was more complex, containing a whole bunch of controls.  How would you set the IsActive property of the viewModel?

Do you know if the example you created is the typical implementation of IActiveAware, and if there are alternative methods of implementing it to address the above 2 concerns?

Thanks again for your help, and your time.  I really appreciate it!

Sincerely,

Jon B

Developer
Oct 31, 2011 at 4:50 PM

Hi Jon,

The fact that the IsEnabled property of the TextBox is bound to the IsActive property of the view model is just a way to demonstrate that the IsActive property is being changed when selecting items in a region, due to the aforementioned behavior of the region. Hence, this property will be set by the region behavior if the view model implements IActiveAware, regardless of its complexity. Therefore you can benefit from this by defining a custom logic in the setter of the IsActive property that suits your scenario. As Brian Noyes shows in his blog, you can also fire the IsActiveChanged event from here, this way you can subscribe to this event to be notified when the IsActive property changes.

Regards,

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


Oct 31, 2011 at 9:11 PM

Hi Agustin,

Interesting!  Thanks for the clarification!  When I had been looking at your sample code before, it had appeared that if I removed the binding of the text box's IsEnabled property to the viewModel's IsActive property, the setting of IsActive in the viewModel stopped working altogether.

Since your reply, I've played with it a bit more, and have come to realize that IActiveAware indeed still does work, but it has an interesting behavior:  If you click inside the textBox (for example, to change the text) and even change the text within, IsActive will not fire.  Even if I go into each and every text box and change the text in each, IsActive never fires.  However, if I click outside and to the right of any given textBox, the IsActive property fires appropriately.

What is it that determines when IActiveAware will fire?  For instance, is it possible to make some modification such that clicking into the text box contained within a particular region would make IActiveAware fire?

Thanks very much!  This is extremely helpful!

Jon

Developer
Nov 1, 2011 at 2:55 PM

Hi,

The SelectorRegionAdapter, as any region adapter, maps a certain control (in this sample, a ListBox, which inherits from Selector) to a region, making it possible to coordinate the display of views within specific regions. In this particular case, this is done through the SelectorItemsSourceSyncBehavior, which is  attached to the region considered by the aforementioned adapter.

This behavior ensures that if you select an item in the control, it will Activate it in the region (so it will now belong to the ActiveViews collection in the region).

On the other hand, the RegionActiveAwareBehavior will monitor the ActiveViews collection, and will update the IsActive property of the IActiveAware views/view models that enter or leave this collection. Thus, the IsActive property of views that belong to a Selector region will be modified when the item is selected or unselected. This should be true regardless of whether you click on the Textbox, or outside it, as long as you select the item in the list box.

I hope you find this useful.

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