Displaying a View inside a Region is about 5-10 seconds slow for the first time, and UI freezes for that period in my Prism Composite WPF application. In subsequent times View is loaded relatively faster without any UI freezing. View is composed of a Devexpress WPF Grid control and data is fetched from a SQL database. I don’t think its an issue with the Grid control / binding though, even if I remove bindings with the grid control, View takes almost same time to render itself into a Region.
This is the code I use to load View into a Region defined inside the Shell:
public Action<MenuModel> LoadViewRequest { get; set; }
public SyncController(IUnityContainer container, IEventAggregator eventAggregator, IRegionManager regionManager)
{
this.container = container;
this.eventAggregator = eventAggregator;
this.regionManager = regionManager;
this.LoadViewRequest = (menuItem) => { LoadRequestedView(menuItem); };
this.eventAggregator.GetEvent<ViewRequestedEvent>().Subscribe(LoadViewRequest, ThreadOption.UIThread, true, i => i.Module == "Sync");
}
private void LoadRequestedView(MenuModel menuItem)
{
try
{
IViewModel viewModel = this.container.Resolve<SynchronizeViewModel>();
this.regionManager.Regions["ViewRegion"].Add(viewModel.View);
this.regionManager.Regions["ViewRegion"].Activate(viewModel.View);
viewModel.DisplayName = menuItem.Description;
this.eventAggregator.GetEvent<ViewNotificationEvent>().Publish(menuItem.Description);
}
catch (ResolutionFailedException) { }
}
What could be the reason behind this behavior? Why View is getting loaded almost instantaneously when loaded for the second time? Does that mean even after removing View from the Region my application hold a reference to View?
Most likely, your View is loading all of the Dev Express and any other Assemblies into memory and JITting them. We see a similar thing in our UI when using the Infragistics WPF Controls.
Edit: You cannot really avoid this. The Assemblies need to be loaded and will be JITed on the first run. You can try pre-loading the Assemblies during start up and pre-jitting the assemblies. You won’t be able to simply execute this on another UI thread, because now your grid will be owned by different thread than your container, so it will not work.
In a Data Grid, you will see a significant amount of time being spent drawing the elements. Usuaully what will happen is that the grid control will create all of the row and cell UI elements and will need to bind those to your data. All in all, a rather large number of WPF UI elements will be created which, inherently, takes up a decent amount of time.