MvvmCross-无法解析ViewModel的类型 [英] MvvmCross - Failed to resolve type for ViewModel

查看:76
本文介绍了MvvmCross-无法解析ViewModel的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与另一个问题有关发布,但可能需要保证.

This is related to another question I posted but probably warrants its own.

我需要能够从其相关的Android View在ViewModel中执行命令,尤其是在单击操作栏菜单项时.因此,首先,我需要访问ViewModel的实例.

I need to be able to execute a Command in my ViewModel from its related Android View and specifically when an Action Bar menu item is clicked. Therefore, first of all I need access to the instance of the ViewModel.

首先,我在视图OnCreateOptionsMenu(IMenu menu)方法中尝试了以下代码行:

Firstly I tried the following line of code in the OnCreateOptionsMenu(IMenu menu) method of the View:

NewJobViewModel newJobViewModel = Mvx.Resolve<NewJobViewModel>();

但是,这导致了错误:"UNHANDLED EXCEPTION: Cirrious.CrossCore.Exceptions.MvxException: Failed to resolve type MyProject.Core.ViewModels.NewJobViewModel"

However, this caused the error: "UNHANDLED EXCEPTION: Cirrious.CrossCore.Exceptions.MvxException: Failed to resolve type MyProject.Core.ViewModels.NewJobViewModel"

然后我尝试了以下方法:

I then tried this instead:

[Activity]
public class NewJobView : MvxActivity {
    private NewJobViewModel _newJobViewModel;

    public NewJobViewModel NewJobViewModel {
        get { return _newJobViewModel; }
        set { _newJobViewModel = value; }
    }

    protected override void OnCreate(Bundle bundle) {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.NewJobView);
        this.NewJobViewModel = Mvx.Resolve<NewJobViewModel>();
    }
}

...这无济于事.

...which didn't help.

但是,将以下内容添加到Apps.cs中可解决该错误,但引起了其他问题,因为它显然没有给我提供MvvmCross框架必须已经在后台初始化的相同ViewModel实例(?):

However, adding the following to Apps.cs cured the error but caused other issues as it clearly hadn't given me the same instance of the ViewModel that the MvvmCross framework must have already initialised in the background(?):

public override void Initialize() {
    CreatableTypes()
        .EndingWith("NewJobViewModel")
        .AsTypes()
        .RegisterAsSingleton();
}

我显然误解了一些体系结构以及背景中发生的事情(因此,如果这是一个基本问题,我深表歉意),但是有人可以告诉我我做错了什么吗?

I clearly misunderstand some of the architecture and what's happening in the background (so I apologise if this is a basic question) but can someone please tell me what I'm doing wrong?

推荐答案

MvvmCross会自动为您连接ViewViewModel.

MvvmCross automatically wires up your View and ViewModel for you.

这在呼叫base.OnCreate(bundle);

如果要在视图中访问ViewModel,则可以使用ViewModel属性来完成此操作.

If you want to access your ViewModel within your View you can do this using the ViewModel property.

但是,由于ViewModel属性是在共享基类中定义的,因此它的类型为IMvxViewModel

However, since the ViewModel property is defined in a shared base class, it is of type IMvxViewModel

要获取键入的ViewModel,您只需将其转换为您的特定类型-例如

To get a typed ViewModel you can simply cast it to your specific type - e.g.

 var myTypedView = (MyTypedViewModel)ViewModel;

您绝对不想创建一个新的视图模型(这是Mvx.Resolve<NewJobViewModel>()试图做的)-相反,您只想访问当前的视图模型.

You definitely don't want to create a new viewmodel (which is what Mvx.Resolve<NewJobViewModel>() tries to do) - instead you just want access to the current one.

这篇关于MvvmCross-无法解析ViewModel的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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