无法在iOS中使用storyboards让MvvmCross工作

3
我正在尝试使用StoryBoards编写一个MvvmCross应用程序。我知道有类似的问题,但是我已经按照解决方案进行了尝试,但无法使其正常工作。我在这个问题中引用了我尝试过的解决方案之一。
我将从头开始讲解我所做的事情,希望这可以帮助诊断我的问题。
  1. Create a project using the the Universal iOS Single View App in Visual Studio.
  2. I do the standard MvvmCross setup, adding nuget, updating AppDelegate etc.
  3. I get it to compile.
  4. I add a new ViewController to the iPhone storyboard. I call it LogonViewController. I set the Class, Storyboard ID and Restoration ID to LogonViewController. The LogonViewController class is generated. I change it to derive it from MvxViewController
  5. I move the entry arrow from the RootViewController (the view controller on the storyboard when it was initially created) to point to the LogonViewController. I leave the RootViewController on the storyboard
  6. I Change the LogonViewController class to derive from MvxViewController and set the Register attribute to LogonView eg

    [Register ("LogonView")]
    partial class LogonViewController
    
  7. I add a label to the LogonViewController just as something to display.

  8. I create a custom container derived from MvxTouchViewsContainers as per the post in

MVVMCross支持Xamarin.iOS Storyboards

问题在于当在自定义容器中调用重写的方法(如下所示)时,我会得到一个无效转换异常,因为它无法将UIKit.UIViewController转换为IMvxTouchView。

我还会遇到算术溢出异常。

protected override IMvxTouchView CreateViewOfType(Type viewType, MvxViewModelRequest request)
{
    return (IMvxTouchView)UIStoryboard.FromName("MyStoryBoard", null)


    .InstantiateViewController(viewType.Name);
}

在跟踪日志中,我得到了以下错误信息。
2015-01-28 18:12:09.833 MyApp[3743:68927] mvx:Diagnostic:  8.61 Setup: LastChance start
[0:] mvx:Diagnostic:  8.61 Setup: LastChance start
[0:] 
2015-01-28 18:12:10.247 MyApp[3743:68927] mvx:Diagnostic:  9.05 Setup: Secondary end
[0:] mvx:Diagnostic:  9.05 Setup: Secondary end
[0:] 
2015-01-28 18:12:10.647 MyApp[3743:68927] mvx:Diagnostic:  9.43 Showing ViewModel LogonViewModel
[0:] mvx:Diagnostic:  9.43 Showing ViewModel LogonViewModel
[0:] 
2015-01-28 18:12:11.049 MyApp[3743:68927] TouchNavigation:Diagnostic:  9.83 Navigate requested
[0:] TouchNavigation:Diagnostic:  9.83 Navigate requested
2015-01-28 18:12:11.254 MyApp[3743:68927] Unknown class LogonViewController in Interface Builder file.
2015-01-28 18:12:23.962 MyApp[3743:68927] -[UIViewController bottomLayoutGuide]: guide not available before the view controller's view is loaded
System.OverflowException: Arithmetic operation resulted in an overflow.
   at Mono.Debugging.Soft.SoftDebuggerAdaptor.TargetObjectToObject(EvaluationContext ctx, Object obj)
   at Mono.Debugging.Evaluation.ExpressionEvaluator.TargetObjectToExpression(EvaluationContext ctx, Object obj)
   at Mono.Debugging.Evaluation.ObjectValueAdaptor.CreateObjectValueImpl(EvaluationContext ctx, IObjectValueSource source, ObjectPath path, Object obj, ObjectValueFlags flags)
   at Mono.Debugging.Evaluation.ObjectValueAdaptor.CreateObjectValue(EvaluationContext ctx, IObjectValueSource source, ObjectPath path, Object obj, ObjectValueFlags flags)
System.OverflowException: Arithmetic operation resulted in an overflow.
   at Mono.Debugging.Soft.SoftDebuggerAdaptor.TargetObjectToObject(EvaluationContext ctx, Object obj)
   at Mono.Debugging.Evaluation.ExpressionEvaluator.TargetObjectToExpression(EvaluationContext ctx, Object obj)
   at Mono.Debugging.Evaluation.ObjectValueAdaptor.CreateObjectValueImpl(EvaluationContext ctx, IObjectValueSource source, ObjectPath path, Object obj, ObjectValueFlags flags)
   at Mono.Debugging.Evaluation.ObjectValueAdaptor.CreateObjectValue(EvaluationContext ctx, IObjectValueSource source, ObjectPath path, Object obj, ObjectValueFlags flags)
System.OverflowException: Arithmetic operation resulted in an overflow.
   at Mono.Debugging.Soft.SoftDebuggerAdaptor.TargetObjectToObject(EvaluationContext ctx, Object obj)
   at Mono.Debugging.Evaluation.ExpressionEvaluator.TargetObjectToExpression(EvaluationContext ctx, Object obj)
   at Mono.Debugging.Evaluation.ObjectValueAdaptor.CreateObjectValueImpl(EvaluationContext ctx, IObjectValueSource source, ObjectPath path, Object obj, ObjectValueFlags flags)
   at Mono.Debugging.Evaluation.ObjectValueAdaptor.CreateObjectValue(EvaluationContext ctx, IObjectValueSource source, ObjectPath path, Object obj, ObjectValueFlags flags)
Unhandled Exception:

System.InvalidCastException: Unable to cast object of type 'UIKit.UIViewController' to type 'Cirrious.MvvmCross.Touch.Views.IMvxTouchView'.

我不知道我在这里做错了什么。有人有什么想法吗?以下是安装的软件版本:
MvvmCross 3.5.0 Visual Studio 2012 Update 4, Xamarin Universal Installer v3.5.0.0 Xamarin Studio 5.7 v5.7.0.661 Xamarin v3.9.236.0
更新:
我采纳了EShy的建议并添加了控制器并将其命名为LogonView,但是我仍然遇到一个问题,即LogonViewModel没有被构造。下面显示了日志。有什么想法吗?
2015-01-29 11:31:33.579 MyApp[12631:273902] mvx:Diagnostic:286.77 Message = [Failed to construct and initialize ViewModel for type MyApp.Core.ViewModels.LogonViewModel from locator MvxDefaultViewModelLocator - check InnerException for more information]
[0:] mvx:Diagnostic:286.77 Message = [Failed to construct and initialize ViewModel for type MyApp.Core.ViewModels.LogonViewModel from locator MvxDefaultViewModelLocator - check InnerException for more information]
[0:] 
2015-01-29 11:31:33.985 MyApp[12631:273902] mvx:Diagnostic:287.17 Stack Trace = [  at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request, IMvxBundle savedState, IMvxViewModelLocator viewModelLocator) [0x00000] in <filename unknown>:0 
  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.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 
[0:] mvx:Diagnostic:287.17 Stack Trace = [  at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request, IMvxBundle savedState, IMvxViewModelLocator viewModelLocator) [0x00000] in <filename unknown>:0 
  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.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_object_EventArgs (object,System.EventArgs)
  at Cirrious.CrossCore.Core.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 (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:62 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:46 
  at MyApp.iOS.Application.Main (System.String[] args) [0x00002] in c:\MyApp.iOS\Main.cs:21 ]
(System.Object sender, System.EventArgs e) [0x00000] in <filename unknown>:0 
  at (wrapper delegate-invoke) <Module>:invoke_void_object_EventArgs (object,System.EventArgs)
  at Cirrious.CrossCore.Core.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 (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:62 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:46 
  at MyApp.iOS.Application.Main (System.String[] args) [0x00002] in c:\MyApp\
MyApp.iOS\Main.cs:21 ]
[0:] 
2015-01-29 11:31:34.393 MyApp[12631:273902] mvx:Diagnostic:287.58 Inner Exception message = [Problem creating viewModel of type LogonViewModel]
[0:] mvx:Diagnostic:287.58 Inner Exception message = [Problem creating viewModel of type LogonViewModel]

如果您将从 UIStoryboard.FromName("MyStoryBoard", null).InstantiateViewController(viewType.Name); 返回的引用存储在变量中,然后使用监视窗口,返回的是哪种类型的控制器? - Stuart
我会做的,Stuart。可能需要一些时间,因为我遇到了一些问题。我还需要将我正在使用的故事板添加到项目设置中吗? - rideintothesun
嗨,Stuart,我按照EShy的建议在故事板中添加了一个视图控制器,并将其命名为LogonView而不是LogonViewController。现在返回的类型是MvxViewController。然而,我仍然遇到了无法构造LogonViewModel的异常。我已经将它作为更新添加到原始问题中。 - rideintothesun
嗨,斯图尔特,自从我将ViewController命名为LogonView后,那个错误现在已经消失了。现在的问题是LogonViewModel无法创建。我已经在这些问题上添加了日志,所以它更清晰易读了。 - rideintothesun
你的LogonViewModel构造函数是什么样子的?它被调用了吗?有没有内部异常可以查看? - Stuart
显示剩余2条评论
2个回答

1
你应该在故事板中将你的视图控制器命名为LogonView而不是LogonViewController。这是MvvmCross正在寻找的viewType.Name。

明天我会试一下看看它是否解决了问题。感谢你的帮助。 - rideintothesun

0
问题在于注入到LogonViewModel中的服务使用了MvvmCross Messenger插件。我没有通过nuget将插件添加到项目中。通知我这一点的异常是内部异常的内部异常。我应该深入挖掘。再次感谢您Stuart提供的帮助和MvvmCross的好处。也感谢EShy提供的命名帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接