title: View is loading very slow for the first time in a Prism(MEF v.5) wpf application
body: <p>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.</p>
  
  <pre><code>private void Navigate(NavigateInfo info)
      {
          _workingNavigateInfo = info;
          _regionManager.RequestNavigate(MAIN_REGION_NAME, new Uri(info.NextViewName, UriKind.Relative), NavigationCompleted);
      }
  </code></pre>
  
  <p>I init a view and viewmodel during app initialization process</p>
  
  <pre><code>var jobB = _container.GetExportedValue&lt;ViewB&gt;();
  var jobBModel = _container.GetExportedValue&lt;ViewBModel&gt;();
  jobB.DataContext = jobBModel;
  </code></pre>
  
  <p>Here an example of my ViewModels </p>
  
  <pre><code>[Export]
  [PartCreationPolicy(CreationPolicy.Shared)]
  public class ViewBModel : NavigationViewModel
  {
      private readonly IRegionManager _regionManager;
      private readonly NavigationService&lt;ViewB&gt; _navigation;
  
      [ImportingConstructor]
      public ViewBModel(IRegionManager regionManager)
      {           
          this._regionManager = regionManager;
          this.GotoA = new DelegateCommand&lt;object&gt;(this.ExecuteGotoA);
          this.GotoBack = new DelegateCommand&lt;object&gt;(this.ExecuteGotoBack);            
          _navigation = new NavigationService&lt;ViewB&gt;(regionManager);
      }
  
      public DelegateCommand&lt;object&gt; GotoA { get; private set; }
      public DelegateCommand&lt;object&gt; GotoBack { get; private set; }
  
      private void ExecuteGotoA(object notused)
      {
          _navigation.NavigateToPage("ViewA");
      }
  
      private void ExecuteGotoBack(object notused)
      {
          _navigation.NavigateBack();
      }
  }
  </code></pre>
  
  <p>and View</p>
  
  <pre><code> [Export] 
  public partial class ViewB : UserControl
  {
      public ViewB()
      {
          InitializeComponent();
      }
  }
  </code></pre>
  
  <p>since navigation didnt work without [Export("ViewB", typeof(ViewB))] attribute, i create a new MefServiceLocatorAdapter to avoid not found error</p>
  
  <pre><code>public class MyMefServiceLocatorAdapter : MefServiceLocatorAdapter
  {
      CompositionContainer _container;
      public MyMefServiceLocatorAdapter(CompositionContainer container): base(container)
      {
          _container = container;
      }
  
      protected override object DoGetInstance(Type serviceType, string key)
      {
          IEnumerable&lt;Lazy&lt;object, object&gt;&gt; exports = this._container.GetExports(serviceType, null, key).ToList();
  
          if ((exports != null) &amp;&amp; (exports.Count() &gt; 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 =&gt; x.ExportDefinitions.Any(y =&gt; y.ContractName.EndsWith(key))).ToList();
          if ((extended != null) &amp;&amp; (extended.Count() &gt; 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));
      }
  }
  </code></pre>
  
  <p>I found a nice article how to make navigation faster <a href="http://vslepakov.blogspot.com/2014/09/navigate-faster-with-prism-and-wpf.html" rel="nofollow">Navigate faster with Prism and WPF</a>
  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 <a href="https://msdn.microsoft.com/en-us/library/microsoft.practices.prism.regions.iregionnavigationcontentloader.loadcontent(v=pandp.50).aspx" rel="nofollow">LoadContent</a> and RequestCanNavigateFromOnCurrentlyActiveViewModel(dont understand why) <strong>run 1 sec</strong>, 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.</p>