什么是处理GoBack的用于不同MvvmCross(V3)平台的最佳方式 [英] What is the best way to handle GoBack for the different MvvmCross (v3) platforms

查看:240
本文介绍了什么是处理GoBack的用于不同MvvmCross(V3)平台的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MvvmCross v3中我使用 ShowViewModel 导航到不同的页面。转换到MVX之前,我会使用 NavigationService.GoBack()办法回到之前的页面。其优点是,该网页无法重新创建。

In MvvmCross v3 I use ShowViewModel to navigate to different pages. Before converting over to Mvx I'd use the NavigationService.GoBack() method to go back to the previous page. The advantage being that the page isn't re-created.

由于 GoBack的方法是特定于平台的WP ,WinRT的,Silverlight的,什么是处理返回到上一个页面所以视图模式仍然独立于平台的?

Since the GoBack method is platform specific to WP, WInRT, Silverlight, what is the best way to handle returning to the previous page so the view model remains platform independent?

一个解决办法可能是使用 ShowViewModel 传递一些数据,该视图可以看到,然后在WP / WinRT中的情况下,从视图中调用 RemoveBackEntry 。但随着MVX,有可能是一个更好的办法。

One solution might be to use ShowViewModel passing some data that the view can see and then in the case of WP/WinRT, calling RemoveBackEntry from the view. But with Mvx, there's probably a better way.

推荐答案

在MvvmCross v3中,我们提供了一个具体的机制,允许的ViewModels发送消息以他们想改变当前演示文稿的用户界面。

In MvvmCross v3, we provided a specific mechanism to allow ViewModels to send messages to the UI that they would like to change the current presentation.

这机制是 ChangePresentation(MvxPresentationHint提示)和它提供消息路由 - 呈现提示 - 从的ViewModels 演示

This mechanism is ChangePresentation(MvxPresentationHint hint) and it provides routing of messages - presentation hints - from ViewModels to the Presenter.

如何演示处理这些消息是平台和应用程序特定的。

How the Presenter handles these messages is platform and application specific.

这消息机制非常普遍,它可能被用于所有类型的在未来的事情 - 例如开发者可能会提供线索它做的事情,比如修改UI布局,用户界面​​,迫使用户把精力放在一定的控制,这导致要显示或隐藏SIP等其中的亮点部分。

This message mechanism is very general and it might be used for all sort of things in the future - e.g. devs might supply hints which do things like change the UI layout, which highlight part of the UI, which force the user focus on to a certain control, which cause the SIP to be displayed or hidden, etc.

有关关闭视图模型的情况下,我们提供 MvxPresentationHint 的特化 - MvxClosePresentationHint - 在基类 MvxViewModel 的一个辅助方法:

For the case of closing a view model, we have provided a specialisation of MvxPresentationHint - MvxClosePresentationHint - and a helper method in a base class of MvxViewModel:

    protected bool Close(IMvxViewModel viewModel)
    {
        return ChangePresentation(new MvxClosePresentationHint(viewModel));
    }

要使用一个ViewModel可以叫关闭(这一点)

To use this a ViewModel can just call Close(this)

当这个叫,你的用户界面中的演示将收到一份关于 ChangePresentation 方法:

When this is called, the Presenter within your UI will receive a message on the ChangePresentation method:

public interface IMvxViewPresenter
{
    void Show(MvxViewModelRequest request);
    void ChangePresentation(MvxPresentationHint hint);
}



对于一般/典型案例 - 其中视图模型这被关闭连接到这是最顶层的活动的看法 / / 的UIViewController ,中MvvmCross默认主持人将能够处理这条消息,并能够以 GoBack的在Windows中,要完成在Android中,并 PopViewController iOS中。

For the general/typical case - where the ViewModel that is being closed is attached to the view which is the topmost Activity/Page/UIViewController, the default presenters within MvvmCross will be able to handle this message and will be able to GoBack in Windows, to Finish in Android, and to PopViewController in iOS.

不过,如果你的用户界面更加复杂的是 - 如如果你想关闭实际上相当于设置页视图模型 >,到飞出,到列SplitView 窗格等,或者如果视图模型相当于比在层次结构中当前最上面的视图中的其他东西 - 那么你就需要提供自定义演示实现 - 而实现将不得不做平台和应用程序特定的逻辑来处理关闭

However, if your UI is more complicated than that - e.g. if the ViewModel you want to Close actually corresponds to a Tab, to a Flyout, to a SplitView pane, etc, or if the ViewModel corresponds to something other than the current topmost view in the hierarchy - then you will need to provide a custom presenter implementation - and that implementation will have to do platform and application specific logic to handle the Close.

上面的提示是什么,我建议你使用...

The above hint is what I recommend you use...

然而,作为替代

如果你觉得这个 ChangePresentation(MvxPresentationHint提示)机制实在是太重量级/矫枉过正您的应用程序,那么你也可以,当然,下降到自定义或消息基础的机制来代替。

If you were to feel this ChangePresentation(MvxPresentationHint hint) mechanism was simply too heavyweight/overkill for your app, then you can also, of course, drop down to a custom or Message based mechanism instead.

一个样本,这是否是CustomerManagement样本 - 它提供了每个平台上的自定义IViewModelCloser实现 - 见:

One sample that does this is the CustomerManagement sample - it provides a custom IViewModelCloser implementation on each platform - see:

  • custom interface - https://github.com/slodge/MvvmCross/blob/v3/Sample%20-%20CustomerManagement/CustomerManagement/CustomerManagement/Interfaces/IViewModelCloser.cs
  • BaseViewModel - https://github.com/slodge/MvvmCross/blob/v3/Sample%20-%20CustomerManagement/CustomerManagement/CustomerManagement/ViewModels/BaseViewModel.cs
  • Droid - https://github.com/slodge/MvvmCross/blob/v3/Sample%20-%20CustomerManagement/CustomerManagement/CustomerManagement.Droid/SimpleDroidViewModelCloser.cs
  • WP - https://github.com/slodge/MvvmCross/blob/v3/Sample%20-%20CustomerManagement/CustomerManagement/CustomerManagement.WindowsPhone/ViewModelCloser.cs
  • Touch - https://github.com/slodge/MvvmCross/blob/v3/Sample%20-%20CustomerManagement/CustomerManagement/CustomerManagement.Touch/CustomerManagementPresenter.cs

这篇关于什么是处理GoBack的用于不同MvvmCross(V3)平台的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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