First time loading a View takes a 2-5 sec dependence a view content. But the second time it is load immediately. The most "heavy" content has only a RadGridView, but assembly and all data (empty data) already loaded from database during initialization.
private void Navigate(NavigateInfo info)
{
_workingNavigateInfo = info;
_regionManager.RequestNavigate(MAIN_REGION_NAME, new Uri(info.NextViewName, UriKind.Relative), NavigationCompleted);
}
I init a view and viewmodel during app initialization process
var jobB = _container.GetExportedValue<ViewB>();
var jobBModel = _container.GetExportedValue<ViewBModel>();
jobB.DataContext = jobBModel;
Here an example of my ViewModels
[Export]
[PartCreationPolicy(CreationPolicy.Shared)]
public class ViewBModel : NavigationViewModel
{
private readonly IRegionManager _regionManager;
private readonly NavigationService<ViewB> _navigation;
[ImportingConstructor]
public ViewBModel(IRegionManager regionManager)
{
this._regionManager = regionManager;
this.GotoA = new DelegateCommand<object>(this.ExecuteGotoA);
this.GotoBack = new DelegateCommand<object>(this.ExecuteGotoBack);
_navigation = new NavigationService<ViewB>(regionManager);
}
public DelegateCommand<object> GotoA { get; private set; }
public DelegateCommand<object> GotoBack { get; private set; }
private void ExecuteGotoA(object notused)
{
_navigation.NavigateToPage("ViewA");
}
private void ExecuteGotoBack(object notused)
{
_navigation.NavigateBack();
}
}
and View
[Export]
public partial class ViewB : UserControl
{
public ViewB()
{
InitializeComponent();
}
}
since navigation didnt work without [Export("ViewB", typeof(ViewB))] attribute, i create a new MefServiceLocatorAdapter to avoid not found error
public class MyMefServiceLocatorAdapter : MefServiceLocatorAdapter
{
CompositionContainer _container;
public MyMefServiceLocatorAdapter(CompositionContainer container): base(container)
{
_container = container;
}
protected override object DoGetInstance(Type serviceType, string key)
{
IEnumerable<Lazy<object, object>> exports = this._container.GetExports(serviceType, null, key).ToList();
if ((exports != null) && (exports.Count() > 0))
{
// If there is more than one value, this will throw an InvalidOperationException,
// which will be wrapped by the base class as an ActivationException.
return exports.Single().Value;
}
var extended = this._container.Catalog.Where(x => x.ExportDefinitions.Any(y => y.ContractName.EndsWith(key))).ToList();
if ((extended != null) && (extended.Count() > 0))
{
var type = ReflectionModelServices.GetPartType(extended.Single()).Value;
var serviceTypeIdentity = AttributedModelServices.GetTypeIdentity(type);
return _container.GetExports(serviceType, null, serviceTypeIdentity).First().Value;
}
throw new ActivationException(FormatActivationExceptionMessage(new CompositionException("Export not found"), serviceType, key));
}
}
I found a nice article how to make navigation faster Navigate faster with Prism and WPF but id doesn't give me any improvements. I used a performance profiler Redgate's ANTS and it shows me that during the first time navigation the methods LoadContent and RequestCanNavigateFromOnCurrentlyActiveViewModel(dont understand why) run 1 sec, but the second time it took less then 1 mls. I tried to do LoadContent during initialization and added to region, but i coudnt load and add all Views to region. And unfortunately this strategy didnt give me any improvements.