Silverlight PRISM,实例化视图并将数据传递到视图模型中
我一直在四处寻找,但还没有找到太多这方面的信息。我设置了一个 PRISM 项目,到目前为止,一切似乎都在工作,在模块的主类中,我正在以编程方式创建我的视图。
我想做的是将对象发送到视图的每个实例的视图模型。目前,我正在尝试将对象传递到视图构造函数中并执行类似以下操作:
public MyView(IUnityContainer container, List<string> myDataObject)
{
InitializeComponent();
MyViewViewModel vm = LayoutRoot.DataContext as MyViewViewModel;
vm.DataObject = myDataObject;
}
这会导致 vm.DataObject 出现 NullReferenceObject,我假设此时视图模型尚未正确实例化。
谁能指导我应该如何完成此操作?数据对象是否应该通过视图发送?
感谢您抽出时间
I have been looking around and haven't found much information on this yet. I have a PRISM project set up and it all seems to be working so far, within the main class of a module I am programatically creating my views.
What I am trying to do is get an object sent through to the viewmodel for each instance of the view. At the moment I am trying to pass the object into the views constructor and do something like this:
public MyView(IUnityContainer container, List<string> myDataObject)
{
InitializeComponent();
MyViewViewModel vm = LayoutRoot.DataContext as MyViewViewModel;
vm.DataObject = myDataObject;
}
This causes a NullReferenceObject for vm.DataObject, I assume the viewmodel hasn't been properly instantiated at this point.
Can anyone guide me on how this should be done? should the data object even be send via the view?
Thanks for your time
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您使用的是 Unity 容器,因此您可以使用它通过视图传递 DataContext 对象。创建 DataContext 时使用
RegisterInstance
,然后在每个适当的视图上使用Resolve
。如果您有多个 DataContext,请使用命名注册。Since you are using a Unity container, you could use it to pass the DataContext object through views. Use
RegisterInstance
when you create the DataContext, then useResolve
on each appropriate view. If you have more than one DataContext, use named registration.在我们的代码以及我观看过的所有视频中,您都进行了设置,以便视图采用 ViewModel 作为参数。
如果您需要从其他地方传递东西,您可以使用 eventAggregator 来订阅和发布它们。
在你拥有 DataObject 的地方有点像这样:
在你想要访问它的地方(可能是虚拟机):
仍然有一些同步的事情需要处理,但是,如果这是你想要的,这应该让你继续前进。
In our code, and in all the videos that I've watched, you set it up so that the view takes, as a parameter, the ViewModel.
If you need to pass things around from somewhere else, you can use the eventAggregator to subscribe and publish to them.
Kind of like this in the spot where you have the DataObject:
And like this where you want to have access to it (probably the VM):
There are still some synchronous things to deal with, but, if this is what you want, this should get you going.
在我编写的示例应用程序中,我创建了一个由视图实现的界面。该接口有 1 个成员:SetViewModel。该方法接受一个对象,即 ViewModel。 ViewModel 为视图提供了绑定位置并处理命令。
该接口在实际视图中的实现如下所示:
视图的构造函数保持默认状态,并且代码隐藏中的唯一代码是此 SetViewModel 方法。
在 ViewModel 类的构造函数中,它调用此方法并将其自身传递给视图。
Module类中的initialize方法向UnityContainer注册类型并将视图添加到区域
In a sample app I wrote I created an interface which was implemented by the view. This interface has 1 member, SetViewModel. This method takes an object, which is the ViewModel. The ViewModel provides the View with places to bind to and handles commands.
the implementation of this interface in the actual view looks like this:
The constructor of the View remains default, and the only code that will ever be in the codebehind is this SetViewModel method.
In the constructor of the ViewModel class, it calls this method and passes itself to the view.
The initialize method in the Module class registers the types with the UnityContainer and adds the view to the region
有没有办法手动创建视图及其视图模型,并在视图模型上设置相关数据对象,以便它们可以进行数据绑定,然后在添加到区域之前将两者绑定在一起?
Is there a way to manually create the view and it's view model and set the relevant data objects on the viewmodel so that they can be data bound, then tie both together before adding to a region?