MvvmCross:ShowViewModel是否总是构造新实例? [英] MvvmCross: Does ShowViewModel always construct new instances?

查看:71
本文介绍了MvvmCross:ShowViewModel是否总是构造新实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我调用ShowViewModel时,都会以某种方式检索ViewModel和请求类型的View,并将它们绑定在一起以在屏幕上显示.什么时候创建,查看和从某个地方的缓存中检索ViewModel和View的新实例?如果总是创建新实例,并且我选择创建自己的缓存以防止出现多个实例,那么如何显示缓存的ViewModel实例?

Whenever I call ShowViewModel, somehow a ViewModel and a View of the requested types are retrieved and are bound together for display on the screen. When are new instances of the ViewModel and View created versus looked up and retrieved from a cache somewhere? If new instances are always created and I choose to make my own cache to prevent multiple instances, then how do I show my cached ViewModel instance?

推荐答案

何时创建新的ViewModel和View实例,而不是查找并从某处的缓存中检索?

When are new instances of the ViewModel and View created versus looked up and retrieved from a cache somewhere?

从不-对于新导航,默认行为始终是创建新实例.

Never - for new navigations the default behaviour is always to create new instances.

如果...如何显示我的缓存ViewModel实例?

if... how do I show my cached ViewModel instance?

如果出于任何原因要覆盖ViewModel的位置/创建,则可以在App.cs中的以下位置找到有关覆盖DefaultViewModelLocator的信息:

If for whatever reason you want to override the ViewModel location/creation, then there's information available about overriding the DefaultViewModelLocator in your App.cs in:

  • MVVMCross Passing values to ViewModel that has 2 constructors
  • http://slodge.blogspot.co.uk/2013/01/navigating-between-viewmodels-by-more.html

简单地说,实现您的代码:

Put simply, implement your code:

public class MyViewModelLocator
  : MvxDefaultViewModelLocator
{
    public override bool TryLoad(Type viewModelType, IDictionary<string, string> parameterValueLookup,
                             out IMvxViewModel model)
    {
        // your implementation
    }
}

然后在App.cs中返回它:

then return it in App.cs:

protected override IMvxViewModelLocator CreateDefaultViewModelLocator()
{
    return new MyViewModelLocator();
}


请注意,诸如>如何在MVVMCross应用程序中替换MvxDefaultViewModelLocator的旧帖子 >在概念上仍然兼容-但这些旧帖子中的详细信息现在已过时.


Note that older posts like How to replace MvxDefaultViewModelLocator in MVVMCross application are still conceptually compatible - but the details in those older posts are now out of date.

这篇关于MvvmCross:ShowViewModel是否总是构造新实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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