2
votes

I am working on a WPF PRISM application that has the following structure (I have simplified to explain better without additional layers). I am using Unity as my DI/IOC

  • AppMain - Bootstrapper
  • Gui - Views and View Models
  • Data - Data using EF.

In Gui, I have views names as below:

  • Home
  • EmployeesView
  • OrdersView
  • Reports

I have three regions in the shell. MainRegion - Main Content TopRegion - Navigation Menu BottomRegion - Status Bar

I am using the following method to register views to the regions

IRegion region = _regionManger.Regions[RegionNames.MainRegion];
var mainView = _container.Resolve<Home>();
region.Add(mainView, ViewNames.HomeViewName);
region.Activate(mainView);

The first of activation happens in the Module Initialize method for Top, Main and Bottom.

After this, I am activating other views when the button are clicked. It is just code behind for now. Sample code here:

 IRegion region = _regionManger.Regions[RegionNames.MainRegion];
 var reportView = region.GetView(ViewNames.ReportsViewName);
 if (reportView == null)
 {
    reportView = _container.Resolve<ReportsView>();
    region.Add(reportView, ViewNames.ReportsViewName);
    region.Activate(reportView);
 }
 else
 {
        region.RequestNavigate(ViewNames.ReportsViewName);
 }

PROBLEM1: Any advise on how this can be done or the way I am doing is fine. The top menu has Home, Employees, Orders, Reports buttons.

In the home page I have recent orders by the employee in datagrid as readonly. I would like to double click to navigate to the OrderView and pass the selected order to show to the user. PROBLEM2 I am not sure where to do the navigation for this.

PROBLEM3: Another issue was if set the RegionMemberLifeTime keepAlive false, INavigationAware methods don't fire. If I don't set the KeepAlive to false, the page does not get refreshed because, the view model does not get called.

I need the pages to refresh when it is navigated to and not be stale and also handle any confirm prompts to the user when the view is navigated away from it.

Your help is very much appreciated.

1
My above comment about regionamemberlifetime was wrong. I think I was having the view name spelling mistake. I also looked at the prism quick starts and samples with PRISM. The navigation is done in the viewmodel. Once I understand, I will correct my posting and the solution. Thanksisakavis

1 Answers

1
votes

it's certainly too late but…

Problem 1/2: is there a particular reason why you add content to region in module initializer? the common way is more like -> in xaml:

<ContentControl prism:RegionManager.RegionName="MainRegion" />

and in ModuleInit.cs -> Initialize()

_regionManager.RegisterViewWithRegion("MainRegion", () => _container.Resolve<MainView>());

Problem 3: the view has to implements INavigationAware, IRegionMemberLifetime and to swich region, in the viewModel you do:

_regionManager.RequestNavigate("RegionWhatever", new Uri("TestView", UriKind.Relative));

But to work you have to register it in ModulInit.cs as an object with viewName, like that:

_container.RegisterType<Object, TestView>("TestView");

and a contentControl with the correct RegionName define in xaml of course