无法使用iOS中的故事板来使用MvvmCross

时间:2015-01-28 18:40:22

标签: ios xamarin storyboard mvvmcross

我正在尝试使用StoryBoards编写MvvmCross应用程序。我知道有类似的问题,但我已经遵循了解决方案,我无法让它工作。我已经引用了我在这个问题中尝试过的解决方案之一。

我将解释我从一开始就做了些什么,希望这有助于诊断我的问题

  1. 使用Visual Studio中的通用iOS单视图应用程序创建项目。
  2. 我做标准的MvvmCross设置,添加nuget,更新AppDelegate等。
  3. 我得到它来编译。
  4. 我在iPhone故事板中添加了一个新的ViewController。我称之为LogonViewController。我将Class,Storyboard ID和Restoration ID设置为LogonViewController。生成LogonViewController类。我将其更改为从MvxViewController
  5. 派生
  6. 我从RootViewController(故事板最初创建时的视图控制器)移动入口箭头指向LogonViewController。我将RootViewController留在故事板上
  7. 我将LogonViewController类更改为从MvxViewController派生并将Register属性设置为LogonView,例如

    [Register ("LogonView")]
    partial class LogonViewController
    
  8. 我将标签添加到LogonViewController中,就像要显示一样。

  9. 我根据

  10. 中的帖子创建了一个源自MvxTouchViewsContainers的自定义容器

    MVVMCross support for 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]
    

2 个答案:

答案 0 :(得分:1)

您应该在故事板LogonView中命名您的viewcontroller而不是LogonViewController。这是MvvmCross正在寻找的viewType.Name。

答案 1 :(得分:0)

问题是注入LogonViewModel的服务使用了MvvmCross Messenger插件。我没有通过nuget将插件添加到项目中。告诉我这个例外是一个内部异常的内部例外。我应该深入挖掘。再次感谢Stuart的帮助和MvvmCross的一般善意。还要感谢EShy的命名帮助。