MVVM Light 工具包的性能不会受到影响吗?
我一直在使用流行的 MVVM Light 工具包:此处来创建我的 Windows Phone 应用程序,并且有一个关于图案。对于创建的每个页面,我们都会创建一个新的视图模型,以保持后面的代码干净并促进关注点分离。然而,ViewModelLocator 的构造函数包含每个视图模型的实例。
ViewModelLocator 的构造函数通常如下所示:
public ViewModelLocator()
{
////if (ViewModelBase.IsInDesignModeStatic)
////{
//// // Create design time view models
////}
////else
////{
//// // Create run time view models
////}
CreateMain();
CreatePage2();
CreatePage3();
CreatePage4();
}
如果应用程序包含一堆页面,即使对于那些可能永远不需要的视图实例化每个 ViewModel 是否会导致性能问题?
我在这里错过了什么吗?
I've been using the popular MVVM Light toolkit: here for creating my Windows Phone applications and had a question regarding the pattern. For every page created, we create a new view model to keep the code behind clean and promote separation of concerns. However The ViewModelLocator's constructor contains an instantiation of each and every viewmodel.
The ViewModelLocator's constructor typically looks like this:
public ViewModelLocator()
{
////if (ViewModelBase.IsInDesignModeStatic)
////{
//// // Create design time view models
////}
////else
////{
//// // Create run time view models
////}
CreateMain();
CreatePage2();
CreatePage3();
CreatePage4();
}
If an application contains a bunch of pages, won't instantiating every ViewModel even for those views that might never be required cause a performance concern?
Am I missing something here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于任何开销或对性能的任何担忧,最好衡量一下开销/问题是什么。
确实,当通过 .Net、C#、Silverlight、XAML、DataBinding 和 MVVMLight 编码 WP7 时,您会插入大量“开销” - 并且大部分开销是为了方便编码器,而不是为了用户的利益。
然而,WP7 CPU、视频协处理器、快速 RAM 和快速固态内存都非常快 - 因此有一些开销的空间,并且您可以使用这些框架来创建令人愉快的、响应灵敏的 WP7 应用程序。
无论如何都要担心性能 - 但最好通过测量来消除这些担忧,以找出需要优化的地方,或者需要在其他 UI 功能背后隐藏缓慢的地方。
一般来说,当我测量时,我发现我的性能瓶颈不是我期望的......我还发现总是存在权衡 - 例如,在您这里关心的问题中,权衡可能是虽然 Locator 构造代码运行速度较慢,后面的查找代码可以运行得更快 - 因此应用程序内导航可以更快,但启动时间会稍微慢一些。
With any overhead or any concern about performance, it's a good idea to measure what the overhead/problem is.
It's true that when coding WP7 through .Net, C#, Silverlight, XAML, DataBinding and MVVMLight then you inserting a lot of "overhead" - and most of that overhead is there for the convenience of the coder, not for the benefit of the user.
However, the WP7 CPU, the video co-processor, the fast RAM and fast SolidState memory are all really quite quick - so there is room for some overhead, and you can use those frameworks to create delightful, responsive WP7 apps.
By all means worry about performance - but its best to drive those concerns through measurement to find out where you need to optimise, or where you need to hide the slowness behind some other UI feature.
Generally when I measure, I find that my performance bottlenecks aren't where I expect... I also discover that there are always trade-offs - e.g. in your concern here, the tradeoff might be that while the Locator construction code runs slower, the later lookup code can run quicker - so in-app navigation can be faster at the expense of slightly a marginally slower startup time.
您不必在构造函数中显式创建对象。您可以将它们推迟到第一次访问时。
无论如何都要像@Stuart所说的那样衡量性能,但通常最好只为您需要的东西创建足够的东西。因此,将创建推迟到属性获取者是最好的。
但是,如果视图模型的收缩很重,那么您可以考虑在构造函数中进行前期构造,同时向用户提供进度条。同样,这取决于测量性能,分析您的设计,看看什么最适合您的需求。
You don't have to create the objects in your constructor explicitly. You can defer them to the first time they are accessed.
By all means measure the performance as @Stuart has said, but it generally is a good idea to only create enough for what you need. So deferring the creation to the property getter is would be best.
However if the constriction of the view models are heavy then you could consider upfrint construction within the constructor while providing a progress bar to the user. Again that depends, measure performance, analyse your design to see what best suits your needs.