第一次加载视图需要2-5秒钟的时间依赖于视图内容。但是第二次立即加载。最“繁重”的内容只有一个RadGridView,但是在初始化过程中已经从数据库中加载了程序集和所有数据(空数据)。
private void Navigate(NavigateInfo info)
{
_workingNavigateInfo = info;
_regionManager.RequestNavigate(MAIN_REGION_NAME, new Uri(info.NextViewName, UriKind.Relative), NavigationCompleted);
}
我在应用初始化过程中初始化一个视图和viewmodel
var jobB = _container.GetExportedValue<ViewB>();
var jobBModel = _container.GetExportedValue<ViewBModel>();
jobB.DataContext = jobBModel;
这是我的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();
}
}
和查看
[Export]
public partial class ViewB : UserControl
{
public ViewB()
{
InitializeComponent();
}
}
由于没有[Export(“ ViewB”,typeof(ViewB))]属性的导航无法正常工作,因此我创建了一个新的MefServiceLocatorAdapter以避免找不到错误
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));
}
}
我找到了一篇不错的文章,介绍如何加快导航速度,使用Prism和WPF导航速度更快,但是id并没有给我带来任何改善。我使用了性能分析器Redgate的ANTS,它向我展示了在第一次导航中,方法LoadContent和RequestCanNavigateFromOnCurrentlyActiveViewModel(不了解原因)运行1秒钟,但是第二次花费不到1毫升。我尝试在初始化期间执行LoadContent并将其添加到区域中,但是我将加载并将所有视图添加到区域中。不幸的是,这种策略并没有给我带来任何改善。
由于Telerik网格,它第一次运行缓慢的原因。渲染网格并加载所有装配体后,第二次加载会快得多。我几乎可以保证,如果您从视图中删除Telerik网格并运行您的应用程序,则该视图将加载得更快。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句