MvvmCross-视图未加载 [英] MvvmCross - View not loaded

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

问题描述

我将一个旧项目升级到Xamarin.iOS Unified和MvvmCross 4.1.

I upgraded an old project to Xamarin.iOS Unified and MvvmCross 4.1.

运行应用程序时,出现以下异常:

When I run the app, I get the following exception:

MvvmCross.Platform.Exceptions.MvxException:未为MyView加载视图

MvvmCross.Platform.Exceptions.MvxException: View not loaded for MyView

我的课程如下:

MyView : MvxViewController<MyViewModel> {}

MvxViewController<T> : UIViewController, IMvxBindingContextOwner, IUIWrappable 
    where T : ViewModelBase

ViewModelBase : MvxViewModel {}

我怀疑我缺少MyView上的某个接口或某些东西来使MvvmCross正常运行,但是我不确定这可能是什么接口.

I suspect I'm missing an interface or something on MyView to enable MvvmCross to operate correctly but I'm not sure what interface this might be.

我怀疑这是因为我已经不得不在视图与其ViewModel之间的映射中进行硬编码,以使MvvmCross能够通过与MyView关联的反射/自动发现来查找ViewModel.也就是说,在发生此错误之前,我无法找到相关的ViewModel错误.

I suspect this because I already had to hard code in mappings between the View and it's ViewModel, to enable MvvmCross to find the ViewModel through reflection/auto-discovery associated with MyView. That is, before this error, I was getting an unable to find associated ViewModel error.

如果我使MyView实现IMvxIosView,该错误就会消失,然后在我的代码中稍后在ViewModel上获得空引用,因此在需要实例化的地方没有实例化.我的印象是,这应该全部由MvvmCross自动处理,但我可能是错的.

If I make MyView implement IMvxIosView, this error goes away, and I then get a null reference on the ViewModel later on in my code, so somewhere that is not getting instantiated where it needs to. My impression was this should all be handled automatically by MvvmCross, but I might be wrong.

我正在查看诸如 https://github.com/MvvmCross/MvvmCross/wiki/Tip-Calc-A-Xamarin.iOS-UI-project

TipView包含的位置

where TipView contains

public new TipViewModel ViewModel {
    get { return (TipViewModel) base.ViewModel; }
    set { base.ViewModel = value; }
}

MyView的基础没有这样的ViewModel属性.我缺少什么访问权限?

MyView's base has no such ViewModel property. What am I missing to have access to this?

TipCalc的示例代码仍在引用Cirrious命名空间,因此可能发生了变化.

TipCalc's sample code is still referencing the Cirrious namespace, so it's possible things have changed.

任何建议都值得赞赏.我是Xamarin和MvvmCross的新手(尽管在Windows/iOS开发中经验丰富),但是在查找最新文档时遇到了麻烦.

Any suggestions are appreciated. I'm new to Xamarin and MvvmCross (although experienced in Windows/iOS development), and I'm having trouble finding up to date documentation.

推荐答案

尝试执行以下操作:

    var vc = this.CreateViewControllerFor(MvxViewModelRequest.GetDefaultRequest(viewModelType);) as MvxViewController;
    vc.OnViewCreate();

vc.OnViewCreate();应该确保您的View已加载.

The vc.OnViewCreate(); should ensure your View is loaded.

然后一行: this.CreateViewControllerFor(MvxViewModelRequest.GetDefaultRequest(viewModelType);) as MvxViewController;

应确保已使用已设置的ViewModel创建ViewController.这样,您不必手动设置.

Should make sure that the ViewController is created with the ViewModel already set. That way you don't have to set it manually.

该方法是MvxCanCreateIosViewExtensionMethods https://github.com/MvvmCross/MvvmCross/blob/4.0/MvvmCross/iOS/iOS/Views/MvxCanCreateIosViewExtensionMethods.cs

-编辑-

我在您的示例代码中注意到的另一件事是此行:

Another thing I noticed in your sample code is this line:

MyView : MvxViewController<MyView> {}

传递给MvxViewController<T>的type参数应该是与MyView关联的ViewModel,而不是MyView本身.因此,例如,您可能会有类似的内容:

The type parameter passed into MvxViewController<T> should be the associated ViewModel for MyView, not MyView itself. So for example, you might have something like:

MyViewModel : ViewModelBase {}

然后:

MyView : MvxViewController<MyViewModel> {}

-更新-

您应该从MvvmCross随附的现有MvxViewController继承. https://github.com/MvvmCross /blob/3c735adc534a5df2d4730e9d58a08f7863c30cee/MvvmCross/iOS/iOS/Views/MvxViewController.cs

You should be inheriting from the existing MvxViewController provided with MvvmCross. https://github.com/MvvmCross/MvvmCross/blob/3c735adc534a5df2d4730e9d58a08f7863c30cee/MvvmCross/iOS/iOS/Views/MvxViewController.cs

这篇关于MvvmCross-视图未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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