User definable regions in Prism 4

Topics: Prism v4 - Silverlight 4
Oct 22, 2010 at 4:13 PM

This isn't so much a specific question as a concept question.

We are creating a Silverlight app and I am looking at Prism4. Our app has what we call a "dashboard" which allows the user to specify various "snapins" that they want displayed on it. 

Basically one of the pages in our app is what we call a "dashboard". We have about 30 views, we call them "snap-ins" that are candidates to be displayed on the dashboard. The dashboard needs to be user definable in that the positions of the "snapins" and the page of the dashboard is configurable. Think of the iGoogle home page... 

Currently, I have a "dashboard.xaml" form with a tabcontrol in the root grid. In the dashboard codebehind I call a RIA service to get the users dashboard setup information. First I dynamically add the users defined pages/tabs to the tabcontrol and each page gets a canvas control added to it. I then dynamically add the snap-ins that the user has defined adding them to the canvas. I also store the position of the snap-in and the page. The snap-ins can be dragged around by the user.

In reading the doc on regions is seems like they are defined ahead of time and the view is loaded into a region. Would it be possible to create the regions on the fly and locate them on a pageframe with a canvas in each page. 

Bottom line, can I create an iGoogle like page (this would only be one page of the whole app) with regions that are positioned at runtime to load the views into? Am I really gaining anything buy using regions/views here over just adding the snapin's (which are user controls) to the canvas in the page?

Bottom line, can I create an iGoogle like page (this would only be one page of the whole app) with regions that are positioned at runtime to load the views into? Am I really gaining anything buy using regions/views here over just adding the snapin's (which are user controls) to the canvas in the page?

I am looking at prism and while the MVVM stuff I will certainly use, I wonder if there is any benefit to using the regions and view loading provided or just stick with the in built silverlight navigation. I'm not sure I see the benefit? 

I know this is a very general question... but I figured I P&P is the place to get some "guidance"

BOb

Oct 22, 2010 at 8:38 PM

Hi Bob,

Nice to see that you started using Prism in your application. Regarding to your question, as the documentation mentioned you would need to think in the container composition and regions when developing Composite Application using Prism.

From the MSDN documentation: You will be creating either a window, page, or user controls that will contain both fixed content and dynamic content. The fixed content will consist of the overall structure of the containing UI element, and the dynamic content will be what is placed inside a region.

For example, you could design a view (these are what your called "snap-ins") with a Canvas control. Then you could dynamically add instances of this view (snap-in) to the MainRegion (dashboard) of your application.

Additionally, as the content placed in the Canvas control is something decided at run-time, I would recommend you to expose this Canvas as a region by creating a Custom Region Adapter. This way, you could compose the Canvas control by adding content (views) dynamically.

If you opt to do not use region, you will lose the UI Composition ability, since the regions are a main component for achieving this when using View Injection or View Discovery.

You could also find interesting the following documentation section on MSDN: XAML Guidelines for Creating a Composite UI

Hope this helps.

Fernando Antivero
http://blogs.southworks.net/fantivero

 



 

Oct 28, 2010 at 3:36 PM

Thanks for your reply. 

>For example, you could design a view (these are what your called "snap-ins") with a Canvas control. Then you could dynamically add instances of this view (snap-in) to the MainRegion (dashboard) of your application. 

Can I position them in MainRegion somehow? Or perhaps I should ask, how are the views positioned when added to this MainRegion?

This sounds like what I would need to do. The snap-ins don't need/use a canvas. I use a canvas in the dashboard so I can position the views (snap-ins) dynamically. So, would I need to add some type of container/control that had a region in it to my dashboard. Then that container would be dynamically positioned it it could load the view that was specified with view discovery?

Can I use Prism for bootstraping/module loading/MVVM communication without using regions for navigation and just using the built in frame controls for navigation?

I'm still a bit unsure what benfit using the Prism navigation with regions offers over the built in SL navigation with frames.  Does the Prism region navigation allow for caching views as the SL navigation framework does?

Thanks for any advice you can provide.

BOb

 

 

Nov 9, 2010 at 5:30 PM

Hi BOb,

Based on my understanding, you will need to expose a Canvas control (Dashboard) as a region. So it requires you to create a custom RegionAdapter for this control. The adapter might provide the necessary logic for positioning views when they are added.

In Prism you could access to the ViewCollection from a particular region. This collection implements INotifyCollectionChanged, so you could subscribe to this and when a views are added, you could position them from the handler method. So your Adapt method might be similar to the following (pseudo-code):

protected override void Adapt(IRegion region, ContentControl canvas)
{
...
region.Views.CollectionChanged +=
(sender, e) =>
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
OrganizeNewSnapIns(e.NewItems, canvas);
}
};
}

Then in the OrganizeNewSnapIns method, you could provide the necessary logic.

There is not guidance on how to implement a solution without Regions in Prism out-of-the-box. But you should be able to use only the necessary mechanism/techniques/guidance in your application. If you decide to do not use Regions in your application, please take into account that you will lose the UI-Composition ability. It means that you will not be able to use View Discovery or View Injection.

I think that you can find the following Karl Shifflett's blog post interesting: Integrating Prism v4 Region Navigation with Silverlight Frame Navigation

Navigation in Prism provides you a Journal, so you can go forward as well as go back. Additionally, it is integrated with the design/technical concepts managed by Prism

Hope this helps.

Fernando Antivero
http://blogs.southworks.net/fantivero