Xamarin.IOS MvvmCross App在iOS9中的ViewDidLoad方法中抛出NullReference异常

时间:2015-09-21 06:40:45

标签: c# xamarin xamarin.ios mvvmcross ios9

我正在使用PCL开发现有的Xamarin iOS应用程序,并使用MvvmCross进行IoC和数据绑定。建立在MvvmCross 3.1.1

之上

该应用程序看起来像一个TabControl应用程序,但它是MainViewCotnroller上的按钮,给它Tab感觉。视图在Storyboard和 MainViewController 中声明,

  

PrepareForSegue(UIStoryboardSegue segue,NSObject sender)方法

覆盖

以根据所选的Segue加载选定的" Tab" 视图 - 如下所示

  if (segue.Identifier == "FirstSegue")
 {
     var dest = segue.DestinationViewController as FirstView;
     dest.Request = new MvxViewModelRequest<FirstViewModel>(null, null, new MvxRequestedBy());
     foreach (UIView v in src.placeHolderView.Subviews)
     {
         v.RemoveFromSuperview();
     }
     dest.View.Frame = new CGRect(0f, 0f, w, h);
     src.placeHolderView.Add(dest.View);
 }

该应用程序在iOS 7.1至8.4的iPhone上运行良好。

就在iOS 9发布之前,我在iOS 6的iPhone 6上为应用程序进行了测试安装,并且应用程序在加载时崩溃了。我检索了设备日志(下面)并分析了问题是在MvvmCross ViewDidLoad方法的内容中。我更新了最新的SDK(XCode 7,Xamarin.IOS 9.0)并遵循Xamarin提供的说明,并使用最新的SDK同样错误地编译了应用程序。将MvvmCross的Nuget包引用更新为3.5.1并仍然存在相同的问题

问题是在the 3.5 MvvMCross Source Code in GitHub

的第44行有一个空引用

注意:这段代码至少自3.1以来没有变化,而3.5.1是MvvmCross nuget中的最新代码

下面的堆栈跟踪(出于安全原因,应用程序名称,项目名称已更改):

   2015-09-21 11:06:26.991 MyApp[1210:60b] Unhandled managed exception:
Object reference not set to an instance of an object (System.NullReferenceException)
  at Cirrious.CrossCore.Mvx.Resolve[IMvxViewModelLoader] () [0x00006] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.MvxViewControllerExtensionMethods.LoadViewModel (IMvxTouchView touchView) [0x00057] in <filename unknown>:0 
  at Cirrious.MvvmCross.Views.MvxViewExtensionMethods.OnViewCreate (IMvxView view, System.Func`1 viewModelLoader) [0x00012] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.MvxViewControllerExtensionMethods.OnViewCreate (IMvxTouchView touchView) [0x00001] in <filename unknown>:0 
  at Cirrious.MvvmCross.Touch.Views.MvxViewControllerAdapter.HandleViewDidLoadCalled (System.Object sender, System.EventArgs e) [0x00007] in <filename unknown>:0 
  at (wrapper delegate-invoke) <Module>:invoke_void_object_EventArgs (object,System.EventArgs)
  at Cirrious.CrossCore.Core.MvxDelegateExtensionMethods.Raise (System.EventH
andler eventHandler, System.Object sender) [0x00004] in <filename unknown>:0 
  at Cirrious.CrossCore.Touch.Views.MvxEventSourceTableViewController.ViewDidLoad () [0x00006] in <filename unknown>:0 
  at MyProject.Touch.MostPurchasedView.ViewDidLoad () [0x00048] in d:\Working\Repo\MyApp\<MyProjectName>\MyProject.Touch\Views\OneOfTheView.cs:31 
  at (wrapper managed-to-native) ObjCRuntime.Messaging:IntPtr_objc_msgSendSuper (intptr,intptr)
  at UIKit.UIViewController.get_View () [0x00030] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/build/ios/native/UIKit/UIViewController.g.cs:2632 
  at MyProject.Touch.HomeViewController.PrepareForSegue (UIKit.UIStoryboardSegue segue, Foundation.NSObject sender) [0x0016a] in d:\Working\Repo\MyApp\MyProject.Mobile\MyProject.Touch\HomeViewController.cs:335 
  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 /Users/builder/data/lanes/2077/d230615b/source/maccore/src/UIKit/UIApplication.cs:74 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0001c] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/UIKit/UIApplication.cs:57 
  at MyProject.Touch.Application.Main (System.String[] args) [0x00001] in d:\Working\Repo\MyApp\MyProject.Mobile\MyProject.Touch\Main.cs:16 
2015-09-21 11:06:26.994 MyApp[1210:60b] critical: Stacktrace:
Native stacktrace:
.....

我在View.cs中调用了以下内容尝试了上述内容:

    public override void ViewDidLoad()
    {
        try
        {
            base.ViewDidLoad();
        }
        catch (Exception ex)
        {
           // throw a toast msg in the UI            
        }
    }

我在 ViewDidLoad 覆盖方法中的所有 View.cs 文件中禁用了所有 base.ViewDidLoad(),以将其带到下一级和同样的问题,但在 MvxSimpleTableViewSource 类的构造函数内

以下设备日志:

Object reference not set to an instance of an object (System.NullReferenceException)
      at Cirrious.CrossCore.Mvx.TryResolve[IMvxTouchSystem] (IMvxTouchSystem& service) [0x00006] in <filename unknown>:0 
      at Cirrious.CrossCore.Touch.Platform.MvxIosMajorVersionChecker.ReadIsIosVersionOrHigher (Int32 target, Boolean defaultValue) [0x00000] in <filename unknown>:0 
      at Cirrious.CrossCore.Touch.Platform.MvxIosMajorVersionChecker..ctor (Int32 major, Boolean defaultValue) [0x00006] in <filename unknown>:0 
      at Cirrious.MvvmCross.Binding.Touch.Views.MvxSimpleTableViewSource..ctor (UIKit.UITableView tableView, System.String nibName, System.String cellIdentifier, Foundation.NSBundle bundle) [0x00000] in <filename unknown>:0 
      at MyProject.Touch.MostPurchasedView.ViewDidLoad () [0x00059] in d:\Working\Repo\MyApp\MyProject.Mobile\MyProject.Touch\Views\OneOfTheView.cs:42 
      at (wrapper managed-to-native) ObjCRuntime.Messaging:IntPtr_objc_msgSendSuper (intptr,intptr)
      at UIKit.UIViewController.get_View () [0x00030] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/build/ios/native/UIKit/UIViewController.g.cs:2632 
      at MyProject.Touch.HomeViewController.PrepareForSegue (UIKit.UIStoryboardSegue segue, Foundation.NSObject sender) [0x0016a] in d:\Working\Repo\MyApp\MyProject.Mobile\MyProject.Touch\HomeViewController.cs:343 
      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 /Users/builder/data/lanes/2077/d230615b/source/maccore/src/UIKit/UIApplication.cs:74 
      at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0001c] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/UIKit/UIApplication.cs:57 
      at MyProject.Touch.Application.Main (System.String[] args) [0x00001] in d:\Working\Repo\MyApp\MyProject.Mobile\MyProject.Touch\Main.cs:16 

简而言之:

仅在iOS 9.1中,MvvmCross 3.1.1和MvvmCross 3.5.1在 MvxViewControllerExtensionMethods class which gets called during ViewDidLoad 的第44行中有一个NullReference问题,但在iOS 8.4及以下版本中运行良好

我刚刚熟悉MvvmCross框架,并且不确定它是否是MvvmCross和iOS 9的问题。

有没有人遇到过这个问题?任何有关这方面的帮助将不胜感激。

注意:MvvmCross示例适用于iOS9。所以我认为它必须与Tab实现有关。

1 个答案:

答案 0 :(得分:0)

有一件事是MvvmCross 3.1.1不是为iOS统一代码构建的。这会阻止您提交自己的应用。 MvvmCross 4.0目前处于测试阶段,但我建议使用它,因为它有很多修复。

如果代码在尝试解析IMvxViewModelLoader时在第44行崩溃,则可能意味着您遗漏了IMvxViewModelLoader所在的某个包。