MVVM 和导航服务
实现任何模式的众多好处之一是在应用程序的不同层之间分离关注点。对于 Silverlight 和 MVVM,我认为 NavigationService 属于 UI。
如果 NavigationService 属于 UI,那么它应该在后面的 XAML 代码中使用,但命令发生在 ViewModel 上。我应该在 ViewModel 中的命令上引发事件并让视图处理该事件并调用导航吗?如果我所做的只是导航到另一个页面,这听起来有点荒谬。我不应该直接处理 UI 事件并从那里导航吗?
查看控制事件-> ViewModel 命令 ->引发事件 ->看法 已处理事件 ->导航
或
查看控制事件->查看已处理的事件 ->导航
One of the many benefits of implementing any pattern is to have a separation of concerns between the different layers in an application. In the case of Silverlight and MVVM it is my opinion that the NavigationService belongs to the UI.
If the NavigationService belongs to the UI then it should be used in the XAML code behind, but the commands happens on the ViewModel. Should I raise an event on the Command in the ViewModel and let the View handle the event and call the Navigation? That sounds a little absurd if all I'm doing is simply navigating to another page. Shouldn't I just handle the UI event directly and navigate from there?
View Control Event -> ViewModel Command -> Raise Event -> View
Handled Event -> Navigationor
View Control Event -> View Handled Event -> Navigation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于此问题,有两种已记录的方法
这种方法由 Jesse Liberty 在 第 3 部分 他的 MVVM清淡汤加坚果系列。他的方法是从命令向视图发送一条消息,指示应该进行导航操作。
这种方法是 Laurent Bugnion 的回应到杰西的帖子。这实现了一个处理由视图模型触发的所有导航操作的服务。
这两种方法仅处理 WP7 应用程序中的导航。然而,它们也可以适应 Silverligt 应用程序。
Jesse 的方法在 SL 中更容易使用,因为它不需要访问根视觉对象。然而,导航代码分布在多个地方,并且需要后台代码来执行实际导航。
Laurent 的方法需要访问根视觉对象 - 用于访问内置导航功能。正如 Laurent 的代码所示,访问此内容在 WP7 应用程序中并不是什么大问题。然而,在 SL 应用中,由于没有周围框架,情况会稍微复杂一些。然而,我已经在我的一个项目中使用附加属性实现了 SL 模式,确实进行了所需的接线 - 因此虽然需要更多工作,但它也可用于 SL。
所以得出结论 - 虽然 Jesse 的方法更容易实现,但我个人更喜欢 Laurent 的方法,因为它是更干净的架构 - 不需要后面的代码,并且功能被封装到单独的组件中,因此位于单个点。
There are two documented approaches to this problem
This approach was put forward by Jesse Liberty in Part 3 his MVVM Ligtht soup to nuts series. His approach is to send a message from the command to the view indicating that a navigation operation should take place.
This approach was Laurent Bugnion's response to Jesse's post. This implements a service that handles all navigation operations triggered by the view models.
Both approaches deal only with navigation in WP7 applications. However, they can be adapted to Silverligt applications too.
Jesse's approach is easier to use in SL as it does not require access to the root visual. However, the navigation code gets distributed in several places and requires code behind to do the actual navigation.
Laurent's approach requires access to the root visual - which is used for accessing the built-in navigation functionality. Getting access to this, as shown in Laurent's code, is no big deal in WP7 applications. In SL applications, however, it is slightly more complicated as there is no sourrounding frame. However, I alreay implented the pattern for SL in one of my projects using an attached property do do the required wiring - so although requires more work, it is usable for SL as well.
So concluding - although, Jesse's approach is easier to implement, personally I prefer Laurent's approach for it is cleaner architecture - there is no code behind required, and the functioality is encapsulated into a separate component and thus located at a single point.
这个问题有点晚了,但它是相关的,并且希望对某人有益。我必须使用 MvvmLight 创建 SL4 应用程序,并希望使用可模拟且可以注入到 ViewModel 中的导航服务包装器。我在这里找到了一个很好的起点:Mix11 中的 Laurent Bugnion 的 SL4 示例代码示例,其中包括导航服务演示:深入了解 MVVM Mix11
这里是实现可与 Silverlight 4 一起使用的可模拟导航服务的基本部分。关键问题是获取对要在自定义 NavigationService 类中使用的主导航框架的引用。
1) 在 MainPage.xaml 中,导航框架被赋予一个唯一的名称,在本例中它将是 ContentFrame:
2) 在 MainPage.xaml.cs 中,导航框架作为属性公开:
3) 导航服务类实现 INavigationService 接口,并依赖 MainPage.xaml.cs 的 NavigationFrame 属性来获取参考导航框架:
A bit late to this question, but it is relevant and will hopefully be of benefit to someone. I had to create a SL4 application with MvvmLight and wanted to use a navigation service wrapper that was mock-able and could be injected into the ViewModel. I found a good starting point here: Laurent Bugnion's SL4 sample code samples from Mix11 which includes a navigation service demo: Deep Dive MVVM Mix11
Here are the essential parts for implementing a mock-able navigation service that can be used with Silverlight 4. The key issue is getting a reference to the main navigation frame to be used in the custom NavigationService class.
1) In MainPage.xaml, the navigation frame is given a unique name, for this example it will be ContentFrame:
2) In MainPage.xaml.cs, the navigation frame is exposed as a property:
3) The navigation service class implements the INavigationService interface and relies on the NavigationFrame property of MainPage.xaml.cs to get a reference to the navigation frame: