本文介绍了在卡利微型和温莎城堡的ViewModels处置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是城堡温莎作为我在卡利微Silverlight应用程序容器。他们称之为WCF服务和一堆其他的东西我的视图模型对象是合理的矮胖。因此,当一个窗口被关闭我要打电话container.Release(视图模型),因此城堡管理整个退役/处置过程中,尊重配置的各个生命周期(如的)。

I'm using Castle-Windsor as my container in a Caliburn-Micro Silverlight app. My ViewModel objects are reasonably chunky as they call WCF services and a bunch of other stuff. Therefore, when a window is closed I want to call container.Release(viewModel) so Castle can manage the whole decommission/disposal process, respecting the various lifecycles configured (as outlined in this post).

在我AppBootstrapper我有覆盖的GetInstance如下:

In my AppBootstrapper I have overridden GetInstance as follows:

protected override object GetInstance(Type serviceType, string key)
{
    if (string.IsNullOrEmpty(key)) return container.Resolve(serviceType);
    return container.Resolve(key, serviceType);
}



不过,我挣扎着拿出调用干净/优雅的方式 container.Release(视图模型)。目前似乎并没有提供这方面的任何挂钩。

But I am struggling to come up with a clean/elegant way of calling container.Release(viewModel). There don't seem to be any hooks available for this.

什么是释放视图模型对象的最简单的方式,从ViewModelLocator在卡利微型应用程序返回的?

What is the simplest way of releasing ViewModel objects returned from ViewModelLocator in a Caliburn Micro app?

推荐答案

您想为每个虚拟机类型的生命周期将会在这里产生影响,所以是不是真的为您提供的上下文正确的答案。

The lifecycle you want for each of your VM types is going to have an impact here, so there is not really a right answer for the context you have provided.

屏幕基类的CM提供了受保护的虚拟无效OnDeactivate(布尔接近); 这是一个良好的开端。为了您的重量级的虚拟机,你应该重写此方法,如果虚拟机正在关闭,截至收盘参数指示,释放任何资源,需要进行处置,这将包括配置资源(如 IDisposable的是相关的),并且还断开,以便它可以通过GC进行清理它的任何引用。

The Screen base class of CM provides you with protected virtual void OnDeactivate(bool close); which is a good place to start. For your heavyweight VMs you should override this method, and if the VM is closing indicated by the close parameter, release any resources (WCF channels etc) that need to be disposed of, this would include disposing the resource (if IDisposable is relevant) and also disconnecting any references to it so that it can be cleaned up by the GC.

我不使用城堡,所以我不能帮你配置的生命周期等方面但是,如果你按照上面的,你是不是将要举行任何东西重物。我认为用正确的生命周期配置,岛城将清理,你不会不来的显式调用自身再次使用任何旧实例发布

I don't use Castle so I can't help you in terms of configuring lifecycles etc. But if you follow the above, you aren't going to be holding on to anything heavy weight. I assume that with the correct lifecycle configuration, Castle will clean up any old instances that you aren't going to use again itself without an explicit call to Release.

这篇关于在卡利微型和温莎城堡的ViewModels处置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 08:34
查看更多