Add views to region

Topics: Prism v2 - WPF 4
Jun 26, 2011 at 8:58 AM

Hi,

My applicatin contains list box which is a region that contains about 50 views.

While inserting the views to the region I noted that adding them takes a while .

I tested the issue by adding 1000 views into prism region in list box  via the add view method  and add same number of views via items.add of the list box.

the results was over 8 minutes to the region and ~0.1 seconds to the listbox,Moreover adding to the list via the items.add when the list box defined as region and doning the same thing without the region definition has the same results as mention above.

Is there something I'm doing wrong or is there a way to improve the views adding to region?

Thanks

Eran

Developer
Jun 27, 2011 at 6:30 PM

Hi Eran,

Adding views to regions implies an additional overhead of creating the ItemMetadata object and adding it to the ViewsCollection of the region, setting the RegionManager attached property, and so forth. Also, by using regions you are using the ItemsControl's ItemsSource property, and when you add the views to the ItemsControl's Items property, which are different approaches that might have different performance implications.

However, based on my personal understanding of the scenario you're mentioning, it shouldn't take ~480ms to add a view into a region. It would be helpful if you could provide us with more information about the way you are adding the views to the region (e.g. if you're using DependencyInjection to obtain the views, if you're using scoped regions, etc).

If you consider this to be blocking, you could create a work item in the issue tracker, notifying the product team of this.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jun 28, 2011 at 6:16 AM
Edited Jun 28, 2011 at 7:29 AM

Hi Guido,
The following 3 methods participate in the views adding in my test unit:

public override void Initialize()
        {
            _regionManager.ChangeView("MainRegion","Module.Views.VehiclesContainerView");
            List  vehicles = GetVehicles();
            Stopwatch  stopWatch = new Stopwatch();
            stopWatch.Start();
            foreach(V_Vehicle vehicle in vehicles)
                CreateVehicleView(vehicle);
            stopWatch.Stop();
            double milsec = stopWatch.Elapsed.TotalMilliseconds ;
            double sec = stopWatch.Elapsed.TotalSeconds;
            double min = stopWatch.Elapsed.TotalMinutes;
         }
  
   private void CreateVehicleView(V_Vehicle vehicle)
        {
            VehicleViewModel vehicleViewModel = new VehicleViewModel(_eventBus, vehicle);
            Views.VehicleView view = _container.GetInstance();
            _regionManager.AddViewToLoadedRegion("VehiclesRegion",view,vehicle.VehicleKey.ToString());
        }
 public void AddViewToLoadedRegion(String region, IView view, String viewCode)
        {
            _regionManager.Regions[region].Add(view, viewCode);
        }
The flow in adding view to list box items is the same as describe above only that instead of the calling to add view to loaded region I call items.add ,I will add the method
in here if you like
Thanks
Eran
Developer
Jun 28, 2011 at 2:54 PM

Hi Eran,

There doesn't seem to be anything in the code you're showing that could be causing extra overhead besides what I've previously mentioned.

Thanks,

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi