MVVM:所有视图的一个 ViewModel 结构与每个视图单独的 ViewModel 结构?

发布于 2024-09-27 00:54:42 字数 323 浏览 4 评论 0原文

我是 MVVM 新手,需要一些帮助。

我的应用程序由许多不同的窗口组成,这些窗口显示控件,允许用户编辑业务层中的数据。

目前,每次用户打开这些窗口之一的新实例时,都会从头开始创建一个 ViewModel 结构(镜像业务层的类和集合)并将数据绑定到窗口上的视图,并且还配置为访问业务层的适当部分。

目前这非常缓慢,我怀疑这是因为应用程序必须等到每次打开窗口时创建并配置所有新的 ViewModel。我还注意到 RAM 也很快被耗尽。

在应用程序启动时创建一个单一的 ViewModel 结构,然后所有窗口和控件都引用这个单一实例,这样会更好吗? MVVM 的约定是什么?

I'm new to MVVM and need a bit of help.

My application consists of a number of different windows which display controls allowing the user to edit the data in the Business layer.

At the moment, each time the user opens a new instance of one of these windows, a ViewModel structure - classes and collections mirroring the Business layer - is created from scratch and databound to the View on the window, and is also configured to access the appropriate parts of the Business layer.

This is quite sluggush at the moment, and I have a suspicion it is because the application has to wait until all the new ViewModels are created and configured every time a window is opened. I also notice the RAM gets munched up quite quickly too.

Would it be better to have a single ViewModel structure which is created when the application starts, and then all windows and controls refer to this single instance? What is the convention for MVVM?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴我心暖 2024-10-04 00:54:42

每个视图一个 ViewModel 是相当标准的。您不想共享 ViewModel 的实例,因为它们(通常)是有状态的。

在得出结论是 ViewModel 的创建导致了这种情况之前,我会更深入地研究一下这种情况。使用工具分析应用程序、设置一些秒表或调试应用程序并查看瓶颈是什么。

One ViewModel per view is pretty standard. You don't want to share instances of ViewModels, since they are (usually) stateful.

I would look deeper into the sluggishness before concluding it's the creation of the ViewModel that's causing it. Profile the application with a tool, set some stopwatches, or debug the app and see what the bottleneck is.

維他命╮ 2024-10-04 00:54:42

每次访问视图时是否都需要重新创建视图模型?

如果不是,那么您似乎使用视图优先方法,也许您应该使用视图模型定位器?

您还可以查看视图模型优先方法,也许这更适合您的应用程序。

do you need to recreate your viewmodels every time you access your views?

if not it seems you use view first approach, maybe you should then use a viewmodel locator?

you can also take a look at viewmodel first approach, maybe this fits more in your application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文