每个视图模型是否应该由单个 ViewModelLocator 处理?
我开始探索 MVVM 之光并开始设计我的不同 viemModel。我已经浏览了很长时间来找出我要找的东西,但我无法得到它:-(。
我有一个示例基于单个 MainViewModel,它在 ModelViewLocator 内处理。在大多数情况下,我们将有多个一个 viewModel,那么所有 viewModel 都应该在单个 ViewModelLocator 文件中定义吗?还是每个视图都必须有一个 ViewModelLocator?
换句话说,我是否需要获取以下内容: MainViewModel->主视图模型定位器 图片视图模型-> PictureViewModelLocator
好的,但还有一个问题: 假设我有 3 个 viewModel,分别创建为 ViewModel1、2 和 3 在 ViewModelLocation 中,我创建了与 MainViewModel 相同的结构,以便创建它的实例,并创建一个主属性来访问模型实例。
我发现的问题是,如果与每个 viewModel 对应的每个视图都设置为自己的数据上下文作为 ViewModelLocator.ViewModelx,则视图实例是在设计时创建的,如果在视图的构造函数期间我需要调用从 WCF 服务获取数据的外部类。它会生成“实例创建错误”。
另一方面,如果在每个视图中我绑定,则不是从 ViewModelLocator.ViewModelx 绑定,而是直接从 ViewModelx 绑定,那么我不会收到该错误并且效果更好。
那么正确的方法和逻辑路径是什么:
1 - MainViewModel 是否应该创建所有其他 viewModel ? 2 - 每个 View 是否必须绑定到 ViewModelLocator 中它自己的 MainStatic 属性? 3 - 每个 View 是否创建自己的 ViewModel 实例?
我所做的方法是,在 ViewModel 构造函数创建期间从外部类返回服务数据的 View 仅当我将其直接绑定到 ViewModel 时才有效,这样做是否有困难?
I am starting to explore the MVVM light and start to design my different viemModels. I have browsed for long time to find out what I am looking for but I could not get it :-(.
One sample I have is based on a single MainViewModel, which is handled inside a ModelViewLocator. In most cases we will have more than one viewModel, so should all viewModels be defined in a single ViewModelLocator file or do I have to have one ViewModelLocator per view?
In other words do I need to get this :
MainViewModel -> MainViewModelLocator
PictureViewModel -> PictureViewModelLocator
Ok fine but sill one question:
let say that I have 3 viewModels that I have create as ViewModel1, 2 and 3
In ViewModelLocation, I have create same structure as the MainViewModel in order to create the instance of it and have create a main property to access Models instance.
The problem I have found is that If each of my Views corresponding to each viewModels is set to is own datacontext as ViewModelLocator.ViewModelx, the view instance is create at design time and it makes me trouble if during the constructor of my view I need to call an external class which get data from a WCF service. It generate an "instance creation error".
In an other hand if in each view I bind then NOT from ViewModelLocator.ViewModelx but instead as directly ViewModelx then I do not get that error and works better.
So what is the properway to do and the logic path :
1 - Does the MainViewModel should create all other viewModel's ?
2 - Does each View must be bound to it's own MainStatic propery in ViewModelLocator ?
3 - Does each View create their own Instance of ViewModel ?
The way I have done is that my View which return service data from an external class during ViewModel constructor creation works only if I bind it directly to the ViewModel, does it have trouble doing it so ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常不需要多个视图模型定位器。常见的方法是创建一个 ViewModelLocator,然后将其添加到 App.xaml 中的
Application.Resources
中,以便可以在应用程序中的任何位置使用它。只需在 ViewModelLocator 中为您使用的每个 ViewModel 创建一个属性。下面是一个使用 MVVM Light 4 (beta) 中的 IoC 容器来实例化视图模型的示例。您也可以不使用 IoC 容器,但在更复杂的场景中,它肯定会简化您的代码:
Usually there is no need for multiple view model locators. The common way is to create one ViewModelLocator which you then add to
Application.Resources
in App.xaml so it's available to be used anywhere in the application. Just create a property in the ViewModelLocator for each ViewModel you're using.Below is an example which is using the IoC container in MVVM Light 4 (beta) to instantiate view models. You could also do without the IoC container but in more complex scenarios it'll definitely simplify your code: