MVVM和NavigationService [英] MVVM and NavigationService

查看:184
本文介绍了MVVM和NavigationService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实现任何模式的众多好处之一是将应用程序中不同层之间的关注点分离.对于Silverlight和MVVM,我认为NavigationService属于UI.

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.

如果NavigationService属于UI,则应在后面的XAML代码中使用它,但命令在ViewModel上发生.我应该在ViewModel的Command上引发一个事件,然后让View处理该事件并调用Navigation吗?如果我只是在导航到另一页,这听起来有点荒谬.我不应该直接处理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?

视图控件事件-> ViewModel命令->引发事件->视图 处理事件->导航

View Control Event -> ViewModel Command -> Raise Event -> View Handled Event -> Navigation

视图控制事件->视图处理事件->导航

View Control Event -> View Handled Event -> Navigation

推荐答案

有两种针对此问题的记录方法

There are two documented approaches to this problem

  1. 使用MVVM Light的消息传递功能实现导航

    这种方法是由Jesse Liberty在 Laurent Bugnion对Jesse帖子的回复.这实现了一个服务,该服务处理视图模型触发的所有导航操作.
  1. Implementing the navigation using MVVM Light's messaging functionality

    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.

  2. Implementing a ViewService that handles the navigation

    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.

这两种方法仅处理WP7应用程序中的导航.但是,它们也可以适用于Silverligt应用程序.

Both approaches deal only with navigation in WP7 applications. However, they can be adapted to Silverligt applications too.

Jesse的方法更容易在SL中使用,因为它不需要访问根视觉.但是,导航代码分布在多个位置,并且需要后面的代码来进行实际的导航.

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的方法需要访问根视觉-用于访问内置导航功能.如Laurent的代码所示,在WP7应用程序中获取访问权限并不重要.但是,在SL应用中,由于没有环绕框架,因此稍微复杂一些.但是,我在我的一个项目中使用附加属性强行限制了SL的模式,它确实完成了所需的布线-因此,尽管需要做更多的工作,但它也可用于SL.

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.

因此得出结论-尽管Jesse的方法更易于实现,但我个人更喜欢Laurent的方法,因为它是更干净的体系结构-不需要任何代码,并且功能被封装到单独的组件中,因此位于单个位置.

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.

这篇关于MVVM和NavigationService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆