SilverlightShow: Working with Prism 4 Part 4: Region Navigation Comments http://www.silverlightshow.net/ Silverlight articles, Silverlight tutorials, Silverlight videos, Silverlight samples SilverlightShow.net http://www.rssboard.org/rss-specification Argotic Syndication Framework 2008.0.2.0, http://www.codeplex.com/Argotic en-US estoychev@completit.com (Emil Stoychev) Re: Working with Prism 4 Part 4: Region Navigation silverlightshow.net should support download resume capability for downloadable file. http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment9936 myomintin http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Mon, 23 Sep 2013 09:07:20 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>Hi Brian</p> <p>I do use static loading of the view model in XAML. What I try to do is to switch the view in the MainContent area, by pushing buttons in a menu. I have followed your example in this article for setting up the views and do something like this:</p> <p>Button 1 click;</p> <p><span style="font-family: consolas; font-size: 13px;"> RegionManager.RequestNavigate(</span><span style="color: rgb(163, 21, 21); font-family: consolas; font-size: 13px;">"MainContent"</span><span style="font-family: consolas; font-size: 13px;">, </span><span style="color: rgb(163, 21, 21); font-family: consolas; font-size: 13px;">"PublicHomeView"</span><span style="font-family: consolas; font-size: 13px;">, PostNavigationCallback);</span></p> <p> </p> <p>Button 2 click:</p> <p><span style="font-family: consolas; font-size: 13px;"> RegionManager.RequestNavigate(</span><span style="color: rgb(163, 21, 21); font-family: consolas; font-size: 13px;">"MainContent"</span><span style="font-family: consolas; font-size: 13px;">, </span><span style="color: rgb(163, 21, 21); font-family: consolas; font-size: 13px;">"DemoCompanyView"</span><span style="font-family: consolas; font-size: 13px;">, PostNavigationCallback);</span></p> <p> </p> <p>In the PostNavigationCallback, I get navigationResult.Result true. </p> <p>When I start up the program and I press the first button, the MainContent shows the correct view. When I press the second button it also shows the expected view, but when both views are present in the RegionManager and I press the buttons, nothing happens, eventhouh it still gives navitationResult.Result true.</p> <p>I actually think, that the RegionManager is doing its job, but the MainContent region somehow needs to be notified, that the content has been changed or something like that?</p> <span style="font-family: consolas; font-size: 13px;"> <p>Is there a way to see, which view are the active in RegionManager?</p> <p> </p> <p>navigationResult.Result</p> </span> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8952 BennyPedersen http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Mon, 11 Feb 2013 11:53:35 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>Hi Benny,</p> <p>How are you hooking up your views and view models? Statically in the XAML? Are you using DataTemplates?</p> <p>I assume you are using RegionManager.RequestNavigate to do the navigation - have you confirmed that you get a success callback? Its an async method. Are you passing a view instance to navigate to or a view model that gets hooked up dynamically with a data template?</p> <p>Its hard to help out with this kind of thing just through comments.</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8951 brian.noyes http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Sun, 10 Feb 2013 17:48:36 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>Navigation does not update screen. Do I need to hook up some PropertyChangedEvent?</p> <p>I have a Silverlight application on the way. I'm trying to set up the navigation as in this article and apparently everything is working. From a menu I switch the content on the maincontent. When I debug, I can see, that all navigation to and from are executed and I get a navigationResult = true. In the RegionManager, I can see, that all the needed views are loaded for that region, but in the end, nothing changes on the screen. Initially the views load and are shown in the MainContent, when I press the menu buttons, but as soon as they are loaded, the actual MainContent are not updated if I call an existing view. The views are set to be CreationPolicy.Shared.</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8950 BennyPedersen http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Sun, 10 Feb 2013 14:50:19 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>Navigation does not update screen. Do I need to hook up some PropertyChangedEvent?</p> <p>I have a Silverlight application on the way. I'm trying to set up the navigation as in this article and apparently everything is working. From a menu I switch the content on the maincontent. When I debug, I can see, that all navigation to and from are executed and I get a navigationResult = true. In the RegionManager, I can see, that all the needed views are loaded for that region, but in the end, nothing changes on the screen. Initially the views load and are shown in the MainContent, when I press the menu buttons, but as soon as they are loaded, the actual MainContent are not updated if I call an existing view. The views are set to be CreationPolicy.Shared.</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8949 BennyPedersen http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Sun, 10 Feb 2013 14:36:07 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>Realized it would be simple to mock that up and give it a try, sure enough it fails. I'm going to look into the scoped Regions, but I am seeing a lot of complexity as you mentioned. I suppose it might even be worth it to not worry with keeping the not-open projects as inactive views in the region. I had been experiencing delay when navigating to the master-detail, but that was before I did a lot of refactoring to use the patterns established with Prism.</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8678 ethan http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Tue, 04 Dec 2012 21:29:52 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>I have a sort of "project" setup in my application where users can store their work (work being items in an "Order" for example). Only one project is active at a time, and there is a master-detail setup for the items in an order. For the case of adding items to an order, I would like for the master area to be a prism region (ItemsControl) allowing me to easily navigate/populate as items are selected/added. For speed's sake, I would like to keep multiple instances of the master-detail view in the main region (instead of creating/removing constantly), only activating the one corresponding to the open project. Would my the ItemsControl master region be violating the unique region name then? Maybe more simply worded, can there be multiple regions that technically have the same name but only one is ever in an active region?</p> <p>I realize this is similar to algodfried's question, but your statement "two instances of the same parent view that contains a region alive at the same time" gave me hope if by "alive" you mean active.</p> <p>Thanks again for this great article series!</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8677 ethan http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Tue, 04 Dec 2012 20:15:35 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>Hi Andrew,</p> <p>Great questions. I do find your comment about documentation and samples strange though - those are the two things that I typically cite as one of the strengths of p&p guidance. Go find me other open source projects out there that have as thorough of documentation as a whole book + online supplements as part of their documentation: http://compositewpf.codeplex.com/releases/view/55580. It comes with two full blown app samples (the Stock Trader RI and the MVVM RI) and close to a dozen QuickStarts for separate features of Prism. That seems like pretty darn good docs and samples to me.</p> <p>In answer to the question on ContentControl and TabControl, there is a shared answer here - Prism has what are called RegionAdapters - the thing that bridges between the specific container control that is tagged as a region and the IRegion API itself (Add, Activate, Remove, etc). The RegionAdapter will maintain its own list of views that have been added to it, and will do the right thing with the container's API to show those views. So for a ContentControl, it allows you to add multiple views, will Activate the first one that you add, and you can Activate other ones to achieve navigation within that ContentControl. Behind the scenes, the ContentControlRegionAdapter has logic that says "Activate to me means set the Content property of the ContentControl to the Activated view". The TabControl has logic that says "each Add means put that view in a tab, and Activate means set that as the SelectedItem". In addition, in the Silverlight case, the TabControlRegionAdapter also has to do some additional gymnastics to get the DataTemplate to marry up the view because implicit data templates don't automatically flow down into the ContentControl that is inside a TabItem in a TabControl.</p> <p>Unfortunately Prism does not do any IDisposable handling of the views or their ViewModels. So you do have to actively manage calling IDisposable at the right time outside the Prism code. That was something the team felt could not be easily generalized in the Prism code since an IDisposable object is not supposed to be used after disposal, we didn't think the framework had enough information to know when the right point was to kill something, it just makes it so you can get it hosted in a container or remove it without being coupled to that container. You could potentially write a custom region adapter (derived from one of the built in ones) that handled that if you wanted to encapsulate a predictable pattern for disposal.</p> <p>Prism has built in RegionAdapeters for ContentControl, ItemsControl, and Selector. So anything derived from those (i.e. ListBox) have a good chance of working as a region. But it all depends on how they go about rendering their contents. A DataGrid for example is not a general purpose container for child items even though it is a Selector. Likewise some of the third party component vendors have controls like Docking containers that may be an ItemsControl, but may not work out of the box with the ItemsControl adapter we have in there. </p> <p>Hope that helps</p> <p>Brian</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8632 brian.noyes http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Fri, 23 Nov 2012 16:18:42 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>I'm often put off by stuff that comes out of the MS Patterns & Practices team, mainly due to lack of good documentation and examples. Your article has certainly demystified Prism navigation, thanks. Just a couple of questions though:-</p> <p>If you were to use a ContentControl to host the customer edit views, I guess you'll only ever see one view at a time, so what happens to the others? Do they just sit around out of sight until the app is closed? Is there a way to dispose of a view or request it to be GC'd when that view is navigated away from?</p> <p>Each time you navigate to a customer edit view it creates a new tab in the tab control (assuming the ID is different each time). Have you written code to do this, or does it happen "automagically" because Prism knows what to do with a tab control? If so, are there any other WPF controls that Prism navigation handles in a similar, special way? I've read somewhere that you can use Prism navigation with (say) a listbox - so would each view appear as an item in the list?</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8628 AndrewStephens http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Fri, 23 Nov 2012 11:05:58 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>Sorry, goes beyond the scope of the article, so seeking help for that here is not the right place. Shoot me an email and I may be able to point you in the right direction.</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8378 brian.noyes http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Wed, 15 Aug 2012 15:38:48 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>Hi Brian</p> <p>Yeah I am trying to get RIA services working together with Prism</p> <p>Ideally I want to have controls inside a controls library and only complete views in the web host project </p> <p>Moving the login control out of the web project and into the controls library works compile wise, but I get the error when I try to place the login control onto a view using the toolbox</p> <p>Cheers</p> <p>Paul</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8377 psaxton2012 http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Wed, 15 Aug 2012 15:23:27 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>LoginForm? Are you mixing this article up with my RIA Services Authentication and Authorization article?</p> <p>Hard to know what the problem is when I have no context of what you did or why.</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8376 brian.noyes http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Wed, 15 Aug 2012 13:13:47 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>Hi Brian </p> <p>I am trying to use your approach of splitting controls into a seperate class library.</p> <p>I want to put the login control onto another control.  The login control is currently inside the silverlight client</p> <p> When I try to add this using the tool box I get the error below</p> <p> Error Dialog<br /> -Cannot create an instance of "LoginForm".<br /> <br /> "Cannot find a Resource with the Name/Key ApplicationResources [Line: 19 Position: 65]"</p> <p>Any ideas please?  I have tried to copy app.xaml etc into the control's project which does seem like a hack and I still get this?</p> <p></p> <p>Paul</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8375 psaxton2012 http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Wed, 15 Aug 2012 09:52:56 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>Nothing else currently planned. If you have something you think would be of general appeal, feel free to suggest. My publishing calendar is booked up for the next couple months, but could try to work something in after that.</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8355 brian.noyes http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Tue, 07 Aug 2012 18:57:44 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>Are you planning to do any more tutorials in this series?</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8354 BenMurray http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Tue, 07 Aug 2012 17:44:11 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>For that scenario you should use the RegisterViewWithRegion way of associating the child view with the region in the parent. If the parent is not currently being shown, then once it is created the child view will be created (if using the overload with a type for the view) or the factory method will be invoked (separate overload). The other variants of region navigation require the region container control to already be in existence when you ask for navigation.</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8335 brian.noyes http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Sat, 28 Jul 2012 21:01:40 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>Brian,</p> <p>If I navigate directly to a child region before the top level region is navigated to, will the top level module automatically load to expose the top level region so the navigation to the child region can occur?</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8326 algodfried http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Fri, 27 Jul 2012 21:59:14 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>Thanks Brian,</p> <p>I think I'll try the Prism navigation to regions implementation and see how it goes.</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8323 algodfried http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Wed, 25 Jul 2012 23:20:11 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>As long as the tab content views are unique, and don't use overly general terms for their child regions, it will work fine. And there is not really any inefficiency in doing so, in fact it adds consistency to the way navigation happens within the app and remains very loosely coupled.</p> <p>With MVVM, it is more of a pattern-based approach where you expose a property or properties from a parent view model that exposes an instance or collection of child view models. You then have a data bound control in the parent view such as a ContentControl (for single reference property) or ItemsControl (for collection property) bound to those view model properties, and use DataTemplates to render the appropriate view for the view model. So navigation happens by changing the parent property references or collection contents.</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8322 brian.noyes http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Wed, 25 Jul 2012 22:55:44 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>Brian,</p> <p>Thanks for the quick response.  My intent is that each parent view plugged into the MainRegion will only occur once. For example, there will never be two Agents tabs.  Any operation concerning Agents will always take place on a single tab.  Based on this, it seems like the region navigation approach will work, but it still might not be the most effective way of implementing the functionality.  If instead I use parent-child view model relationships, can I take advantage of some form of navigation awareness so that I can make sure that records are saved before navigation to a new record?</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8321 algodfried http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Wed, 25 Jul 2012 22:38:04 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>Hi algodfried,</p> <p>Unfortunately the short answer is - possibly not without a great deal of complexity. Regions work well for top level navigation and plugging in of views like you describe. They actually support having regions N-levels deep (View A contains Region B into which you plug View C which contains Region D into which you plug View E, etc). The only problem is that regions have to be unique. So as soon as you have two instances of the same parent view that contains a region alive at the same time - typically in two tabs within the UI, regions break. </p> <p><br /> </p> <p>So if each of the parent views you will plug into your MainRegion that is a TabControl are unique with their own child Regions that have different Region names from all the other top level views being plugged in, you will be fine. But for example if you were going to plug in MasterDetailViewA and MasterDetailViewB and they both had two child regions called MasterRegion and DetailsRegion, you are in trouble.</p> <p>It is possible to support this, but it requires creating scoped dependency injection containers so that there is a separate RegionManager per tab and down you slide on the slope of complexity.</p> <p>Generally I stick to using Regions to plug in and navigate between my top level views, occasionally to swap content in some child region in a view I know there will only ever be one instance of, but otherwise I use MVVM composition (parent-child view model relationships) to handle child view navigation.</p> <p>Brian</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8320 brian.noyes http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Wed, 25 Jul 2012 21:36:01 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>I am planning on porting an MS Access/SQL Server application to Silverlight using Prism, MEF,  and MVVM. I would like to emulate the UI functionality as closely as possible.</p> <p>The UI would be structured as follows:</p> <ol> <li>The shell would contain two regions: NavigationRegion and MainRegion <ol> <li>NavigationRegion would consist of PBs to navigate to views in TabItems in the MainRegion TabControl.</li> </ol> </li> <li>Each view would be contained within its own OnDemand module.</li> <li>Each view would have a DataGrid and a Detail Form working as Master/Detail.</li> </ol> <p>My question is, is it possible to configure each Master/Detail view to use Prism navigation using a region in the view as the target for the Detail Form?  If so, would this be better than simply using commands to synchronize the Detail Form to the selected item in the DataGrid?</p> <p> </p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8319 algodfried http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Wed, 25 Jul 2012 19:30:57 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>Thanks again. I will implement the <span style="font-family: 'segoe ui', helvetica, arial, verdana, tahoma, sans-serif; font-size: 12px; line-height: 20px; background-color: #f7f7f7;">IConfirmNavigationRequest and see the result. If no change happens i will try to implement the solution of context sharing ( impleminting the context in anathor shared class, ind inject it like singleton with MEF in all  ViewModel)!</span></p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8087 SefiAbderrahmen http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Tue, 22 May 2012 13:49:22 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>Thanks again. I will implement the <span style="font-family: 'segoe ui', helvetica, arial, verdana, tahoma, sans-serif; font-size: 12px; line-height: 20px; background-color: #f7f7f7;">IConfirmNavigationRequest and see the result. If no change happens i will try to implement the solution of context sharing ( impleminting the context in anathor shared class, ind inject it like singleton with MEF in all  ViewModel)!</span></p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8086 SefiAbderrahmen http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Tue, 22 May 2012 13:16:26 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>I think the right way to address this would be to use the navigation capabilities (implement IConfirmNavigationRequest) in the main view that you are navigating away from that there might be dirty data in. In that implementation, you could choose whether to prompt the user or possibly just automatically persist the changes before navigating away. That would mean you would have to issue the SubmitChanges call AND wait for the callback to know it is complete before allowing navigation to proceed.</p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8080 brian.noyes http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Mon, 21 May 2012 15:20:57 GMT Re: Working with Prism 4 Part 4: Region Navigation <p>Hi Brian,</p> <p> <span lang="en" id="result_box"><span class="hps">in the</span> <span class="hps">third</span> <span class="hps">part, each</span> <span class="hps">edition</span> <span class="hps">of an object is</span> <span class="hps">directly</span> <span class="hps">impacted</span> <span class="hps">in the datagrid</span>. <span class="hps">But</span> <span class="hps">using</span> <span class="hps">the</span> <span class="hps">navigation</span>, <span class="hps">this update</span> <span class="hps">is no longer visible only</span> <span class="hps">after</span> <span class="hps">a</span> <span class="hps">refresh</span>. <span class="hps">My question</span> <span class="hps">is</span> <span class="hps">there a</span> <span class="hps">way to</span> <span class="hps">bring up</span> <span class="hps">the change</span> <span class="hps">immediately</span> <span class="hps">in</span> <span class="hps">the</span> <span class="hps">datagrid of </span> <span class="hps">mainregion</span> <span class="hps">or how to</span> <span class="hps">refresh</span> <span class="hps">the</span> <span class="hps">page by using</span> <span class="hps">the</span> <span class="hps">navigation</span>?</span> </p> <p>(<span lang="en" class="short_text" id="result_box"><span class="hps">I added the</span> <span class="hps">contexte.submit</span> <span class="hps">change ()</span> <span class="hps">to persist</span></span> and i used the navigation aware but the probleme is not solved!)</p> <p> thanks. </p> http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx#comment8078 SefiAbderrahmen http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx Mon, 21 May 2012 12:15:35 GMT