简体   繁体   中英

View is loading very slow for the first time in a Prism(MEF v.5) wpf application

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.

The reason it runs slow the first time if because of the Telerik grid. Once the grid has been rendered and all assemblies loaded, the second time it loads is much faster. I can almost guarantee you that if you remove the Telerik grid from your view and run your app, the view will load much faster.

I'm with Brian on this. You are using Telerik, if you were to use the similar product from DevExpress (I'm speaking of experience here) you would experience the same thing, it has nothing to do with Prism.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM