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

查看:25
本文介绍了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:

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

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();
}

<小时>

请注意,像 如何在 MVMCross 应用程序中替换 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天全站免登陆