ViewModel LifeCycle,何时处置? [英] ViewModel LifeCycle, when does it get disposed?

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

问题描述

在mvvmcross v3 ViewModel中

In mvvmcross v3 ViewModel

public class TimerViewModel : MvxViewModel
{
    System.Timers.Timer timer;

    public TimerViewModel()
    {
        timer = new System.Timers.Timer(500f);

        timer.Elapsed += HandleTimerElapsed;

        timer.Start();

    }

    void HandleTimerElapsed (object sender, ElapsedEventArgs e)
    {
        Debug.Log( "Time Elapsed" );
    }
}

由于MvxViewModel没有实现IDisposable,因此应将以下代码放在哪里?

As MvxViewModel doesn't implement IDisposable, where should I put the following code ?

timer.Stop();
timer.Elapsed += HandleTimerElapsed;

我发现mvvmcross代码具有一些MvxWeakEventSubscription,它用于解决我的问题吗?

I find that mvvmcross code have some MvxWeakEventSubscription, is it used to solve my problem ?

推荐答案

尚不存在一种简单的通用方法来知道何时dispose ViewModel-特别是一旦您开始混合并匹配ViewModel表示样式以包括导航,选项卡,拆分视图,弹出窗口,片段,列表等,并且随着您包括越来越多的平台

There's no easy universal way to know when to dispose the ViewModel - especially once you start mixing and matching ViewModel presentation styles to include navigations, tabs, splitviews, flyouts, fragments, lists, etc. and as you include more and more platforms

因此,我过去有几种关闭计时器的方法是:

As a result of this, a couple of ways I have shut things like timers down in the past are:

1..有时候,我在ViewModel上使用了专门的接口,并确保在每个客户端上都可以正确调用它.

1. Sometimes I have used a specialised interface on the ViewModel and I ensure this is called appropriately on each client.

例如,我已经使用以下方法完成了页面"级视图的一些启动/停止操作:

For example, I have done some starting/stopping of 'page' level Views using:

  • Android中的OnPause/OnResume
  • 在Windows中
  • OnNavigatedTo/OnNavigatingFrom
  • iOS中的ViewDidAppear/ViewWillDisappear
  • OnPause/OnResume in Android
  • OnNavigatedTo/OnNavigatingFrom in Windows
  • ViewDidAppear/ViewWillDisappear in iOS

我已经考虑过将其添加为通用模式来执行此操作(它已登录 https: //github.com/slodge/MvvmCross/issues/74 )-但到目前为止,我还没有将其添加到v3,因为我认为这会导致用户之间的误解-最好让他们这样做在极少数需要的情况下可以做到这一点.

I have thought about adding this as a generalised pattern to do this (it is logged in https://github.com/slodge/MvvmCross/issues/74) - but so far I've not added this to v3 as I think it would lead to too much misunderstanding among users - it's better to let them to do this in the very few situations where it's needed.

更新:我已经在此发布了博客,并发布了一个示例-请参见

Update: I have blogged about this and published a sample - see http://slodge.blogspot.co.uk/2013/11/n42-is-my-viewmodel-visible-can-i-kill.html

2.有时,我刚刚通过MvvmCross Messenger使用了事件聚合-并且我使用了其基于WeakReference的固有消息传递功能,以确保在查看视图时可以对ViewModel进行垃圾收集已经完成了.

2. Sometimes I've just used Event Aggregation through the MvvmCross Messenger - and I've used it's inherent WeakReference-based messaging to make sure the ViewModel can be garbage collected when the view has finished with it.

InternetMinute示例中有一个示例-该示例具有一个滴答生成服务",ViewModel可以通过消息传递连接该滴答生成服务以进行更新-请参阅:

An example of this is in the InternetMinute sample - which has a single 'Tick generation service' which ViewModels can connect to via messaging for updates - see:

  • the service - https://github.com/slodge/MvvmCross-Tutorials/tree/master/InternetMinute/InternetMinute.Core/Services/Tick
  • a ViewModel that consumes the service - via https://github.com/slodge/MvvmCross-Tutorials/blob/master/InternetMinute/InternetMinute.Core/ViewModels/HomeViewModel.cs

您可能会认为这效率低下-因为即使ViewModel不存在也会生成Tick消息-但这只是一个很小的效率低下.

You might consider this slightly inefficient - as the Tick messages will be generated even if the ViewModel isn't present - but it's only a small inefficiency.

3.我考虑过使用更多的最终事件-诸如OnNavigatingFrom(BACK)和'onDestroy'之类的东西以及对UINavigationController委托的一些'最终'检测...但是我还没有一个为何在任何项目上都做到真正".

3. I've considered using more final events - things like OnNavigatingFrom(BACK) and 'onDestroy' and some 'final' detection on the UINavigationController delegates ... but I've not had a reason to do this 'for real' on any project yet.

这篇关于ViewModel LifeCycle,何时处置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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