MvvmCross - 加载ViewModel时出错

时间:2013-09-08 17:30:27

标签: mvvmcross

我正在测试现有的Monotouch项目中的MvvmCross。 我想开始将项目迁移到Mvvm。

我可以启动应用程序并显示我的第一个(mvvm)屏幕。 在那之后,我正在做不同的(非mvvmcross)事情(加载视图等),我想稍后移植。

我现在正尝试将我的一个观点移植到mvvmcross。 我创建了一个View“公共类TestView:MvxViewController”和一个ViewModel“公共类TestViewModel:MvxViewModel”

加载该视图时,应用程序崩溃并出现以下错误:

---Message:
Object reference not set to an instance of an object
---Stack Trace:
  at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request, IMvxBundle savedState) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.MvxViewControllerExtensionMethods.LoadViewModel (IMvxTouchView touchView) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.MvxViewControllerExtensionMethods+<OnViewCreate>c__AnonStorey3.<>m__9 () [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Views.MvxViewExtensionMethods.OnViewCreate (IMvxView view, System.Func`1 viewModelLoader) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.MvxViewControllerExtensionMethods.OnViewCreate (IMvxTouchView touchView) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.MvxViewControllerAdapter.HandleViewDidLoadCalled (System.Object sender, System.EventArgs e) [0x00000] in <filename unknown>:0 
  at (wrapper delegate-invoke) <Module>:invoke_void__this___object_EventArgs (object,System.EventArgs)
  at Cirrious.CrossCore.Touch.Views.MvxDelegateExtensionMethods.Raise (System.EventHandler eventHandler, System.Object sender) [0x00000] in <filename unknown>:0 
  at Cirrious.CrossCore.Touch.Views.MvxEventSourceViewController.ViewDidLoad () [0x00000] in <filename unknown>:0 
  at CatalogMVVMTest.Touch.Views.TestView.ViewDidLoad () [0x0001a] in /Users/matthiasvalcke/Projects/MVVMDemos/CatalogMVVMTest/CatalogMVVMTest.Touch/Views/TestView.cs:20

...

调用“base.ViewDidLoad();”

时似乎崩溃了

不确定这是否很重要,但在加载第一个视图后,我正在以导航堆栈的根目录加载一个新的(非mvvmcross)视图。

所以现在我正在尝试将MvvmCross视图加载到普通的UIView(控制器)

我也正在关注Mvx诊断输出:

mvx: Diagnostic:   1.08 Request is null - assuming this is a TabBar type situation where ViewDidLoad is called during construction... patching the request now - but watch out for problems with virtual calls during construction

编辑:

如果我只是从MvvmCross编辑一个简单的样本,我也会得到它。例如,如果我只使用一个“FirstView”和“FirstViewModel”来获取基本样本,并且我做了类似的事情,我就会收到错误:

var secondv = new SecondView ();
secondv.View.Frame = new RectangleF (0,250,320,100);
Add (secondv.View);

SecondView只是一个MvxViewController。好像我无法在代码中将MvxViewController中的View添加到另一个MvxViewController。这是对的吗?

任何有想法的人?

2 个答案:

答案 0 :(得分:3)

好的,而不是这样做:

var secondv = new SecondView (); //(this is a MvxViewController
secondv.View.Frame = new RectangleF (0,250,320,100);
Add (secondv.View);

我必须这样做(我在tabbar示例中找到它):

var viewModel = new SecondViewModel ();
var secondv = this.CreateViewControllerFor(viewModel) as UIViewController;
secondv.View.Frame = new RectangleF (0,250,320,10);
secondv.View.AutoresizingMask = UIViewAutoresizing.None; //was needed because mvvm is enabling autostretch automatically? 
Add (secondv.View);

对不起,我在其他UIViewControllers上使用了很多UIViewControllers,并且很难理解完整的MvvmCross框架: - )

我仍然在跟踪诊断输出,但是没有nullpointer,它可以工作:

mvx: Diagnostic:   0.11 Request is null - assuming this is a TabBar type situation where ViewDidLoad is called during construction... patching the request now - but watch out for problems with virtual calls during construction

感谢您的帮助!

答案 1 :(得分:0)

您最有可能在Xamarin的最新版本中遇到错误。

https://github.com/slodge/MvvmCross/commit/5786d4964e4262cbf91661f04427e19648acf7df

解决了这个问题

尝试升级到mvvmcross 3.0.11-beta1版本或更高版本 - 或者回滚您的xamarin版本。

有关http://forums.xamarin.com/discussion/comment/26049/#Comment_26049

的更多信息