在哪里实例化我的视图模型对象?
我正在编写一个小型应用程序来学习 WPF/MVVM,但我遇到了一个难题,我有一个视图模型对象可供使用,但我不知道在哪里实例化它。
表示单个窗口数据(一些滑块值、进度条值和一些文本字符串。其中一些直接附加到公开的模型,其他则在视图模型中以避免向模型添加新功能。)
视图模型 一次只需要其中 1 个对象(每个窗口,但我只允许 1 个窗口),尽管它实际上不是单例。但它会在窗口的生命周期内存在(这正常吗?)
所以我的问题是:我是否应该将视图模型实例化为 App.Xaml 中的静态资源,作为 App.xaml.cs 类的成员隐藏代码(在重写的“OnStartup”方法内部)或作为 Window.xaml 中的资源,或作为 Window.xaml.cs 中的对象。
我见过人们把它作为本地资源和启动中的全局对象,但对我来说,它似乎不应该出现在后面的代码中(我所做的就是将它扔到空中,一旦它出现在代码中)实际上,这就是它的全部意义)。
那么关于视图模型应该在哪里实例化的想法呢?
I am writing a small application to learn WPF/MVVM and I have sort of run into a conundrum, I have a viewmodel object ready to go, but I don't know where to instance it.
The viewmodel represents a single windows data(some sliders values, a progressbar value and a few textstrings. Some of these are directly attached to the model which is exposed, others are in the viewmodel to avoid adding new functionality to the model.)
I will only ever need 1 of these objects at a time(per window, but I am only allowing 1 window), although it really isn't a singleton. It will exist for the lifetime of the window though(is that normal?)
So my question is this: Should I instance the viewmodel as a static resource in the App.Xaml, as a member of the App.xaml.cs class in the code behind(inside of an overridden "OnStartup" method) or as a resource in the Window.xaml, or as an object in the Window.xaml.cs.
I have seen people put it as a local resource and as a global object in the startup, but to me it seems like it shouldn't be in the code behind(all I am doing is throwing it up in the air, once it's in existance it can take care of everything else. That's the whole point of it, actually).
So thoughts on where the viewmodel should be instanced?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
窗口的视图模型仅在其窗口的生命周期内存在是完全正常的。 放入: 一样简单
创建它就像在
MainWindow
的构造函数中 。在没有令人信服的理由不这样做的情况下,我就是这样做的。 (如果窗口需要在其事件处理程序中与视图模型进行互操作(有时会这样做),我将为该对象创建一个私有字段,这样我就不必继续转换DataContext
在所有事件处理程序中。)通常,视图模型需要与一个或多个域对象进行互操作。这种情况下的挑战是想出一种方法来告诉视图模型有关该对象的信息,而无需将域对象耦合到创建视图模型的窗口。这是您开始使用服务和服务定位器以及可模拟对象等的地方。
但即使您这样做,您仍然可以简单地在窗口的构造函数中创建视图模型,例如:
我不在窗口的构造函数中创建窗口的视图模型的唯一一次是当其他一些对象 - 就像另一个窗口中的命令 - 正在创建窗口并设置其 DataContext。
It's completely normal for a window's view model to exist only for the lifetime of its window. Creating it can be as simple as putting:
in the constructor for
MainWindow
. That's how I do it in the absence of a compelling reason not to. (If the window's going to need to interoperate with the view model in its event handlers, which it sometimes does, I'll create a private field for the object so that I don't have to keep castingDataContext
in all the event handlers.)Usually, the view model needs to interoperate with one or more domain objects. The challenge in that case is coming up with a way of telling the view model about that object without coupling the domain object to the window that's creating the view model. This is where you start screwing around with services and service locators and mockable objects and the like.
But even when you do this, you can still create the view model simply in the window's constructor, e.g.:
The only time I don't create a window's view model in the window's constructor is when the some other object - like a command in another window - is creating the window and setting its
DataContext
.我通常采用 ViewModelLocator 的概念。
ViewModelLocator 是 App.xaml 中的 StaticResource。
ViewModelLocator 使用 IoC 进行依赖注入(Ninject/Unity/...),并且可以在 DI 中为 ViewModel 的生存时间定义一个范围...
还有一些与 MEF 和ViewModelLocator 概念...
I usually go by the concept of a ViewModelLocator.
The ViewModelLocator is a StaticResource in App.xaml.
The ViewModelLocator uses leverages IoC for Dependecy injection (Ninject/Unity/...) and it is possible to define a scope in the DI for the Livetime of your ViewModel...
There are also some blogposts out there which work with MEF and the ViewModelLocator Concept...