简体   繁体   English

如何从视图模型导航到视图

[英]How to navigate to a view from the viewmodel

I'm wondering if someone could give me some guidance on a Silverlight problem I'm having? 我想知道是否有人可以给我一些有关Silverlight问题的指导?

What I am doing now is building a page in Silverlight using the MVVM pattern to retrieve and display my data. 我现在正在做的是使用MVVM模式在Silverlight中构建页面来检索和显示我的数据。 My page will be a standard "List" type page that uses the datagrid to display and filter data. 我的页面将是一个标准的“列表”类型页面,该页面使用datagrid来显示和过滤数据。

What I would like to do is, If the user clicks an edit button on this list page, I would like to navigate to a details page which will contain a dataform that I want to bind to the same collection of data that is bound to the datagrid of the list page. 我想做的是,如果用户单击此列表页面上的“编辑”按钮,我想导航到一个详细信息页面,该页面将包含我要绑定到绑定到该数据集的同一数据集的数据格式。列表页面的datagrid。 What I'm thinking has to happen is the List ViewModel has to create the Details Page and set the Details Page datacontext to itself (being the instance of the List ViewModel). 我想发生的事情是List ViewModel必须创建“ Details Page”并将“ Details Page”数据上下文设置为其自身(作为List ViewModel的实例)。 I'm not sure how I would create the view, navigate to it and pass the reference to the view model. 我不确定如何创建视图,导航到视图并将引用传递给视图模型。

You may be wondering why i would want a details page that takes a collection. 您可能想知道为什么我要一个需要收集的详细信息页面。 This is so I can simulate behaviour that my user is familiar with in their current access application. 这样一来,我就可以模拟用户在当前访问应用程序中熟悉的行为。 Mostly, it comes in handy when the user filters the list view they are able to enter a page where they could navigate and edit any of the results from their filter without having to open and close the details page repeatedly. 通常,当用户筛选列表视图时,它可以派上用场,他们可以进入一个页面,在该页面中可以浏览和编辑其筛选器中的任何结果,而不必反复打开和关闭详细信息页面。

MVVM is well suited to this. MVVM非常适合于此。 You are right in assuming they both share the same view model, as the data is the same and you can automatically update the data in both locations at once. 假设它们共享相同的视图模型是正确的,因为数据是相同的,并且您可以一次自动更新两个位置的数据。

However, I would avoid getting the view model to create the views. 但是,我将避免使用视图模型来创建视图。 This is often seen as the wrong way to implement MVVM as it creates a coupling from the view models to the views which may get you into trouble later. 通常将其视为实现MVVM的错误方法,因为它会创建从视图模型到视图的耦合,这可能会在以后给您带来麻烦。

A solution with sample code is described in this blog post. 博客文章中介绍了带有示例代码的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM