0

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

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

我现在正试图将我的一个观点也移植到 mvvmcross。我创建了一个视图“公共类 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(Controller)

我也得到以下 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 添加到另一个 MvxViewController。这个对吗?

有人有想法吗?

4

2 回答 2

3

好的,而不是这样做:

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

我必须这样做(我在标签栏示例中找到了它):

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 框架:-)

我仍然得到以下诊断输出,但不再有空指针,它可以工作:

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

谢谢您的帮助!

于 2013-09-10T19:22:14.320 回答
0

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

这已在https://github.com/slodge/MvvmCross/commit/5786d4964e4262cbf91661f04427e19648acf7df中得到解决

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

有关http://forums.xamarin.com/discussion/comment/26049/#Comment_26049的更多信息

于 2013-09-08T19:41:43.597 回答