MonoTouch.Dialog crash

后端 未结 1 329
忘掉有多难
忘掉有多难 2021-01-24 11:37

I have a small test app which just cycles between 3 pages. Here is AppDelegate:

    public override bool FinishedLaunching (UIApplication app, NSDictionary optio         


        
1条回答
  •  不知归路
    2021-01-24 12:13

    Avoid patterns like:

    var navigationController = new UINavigationController(dialogViewController);
    View.Add (navigationController.View);
    

    because navigationController won't be referenced (on the managed side) once the View.Add call is done and the garbage collector can dispose it (whenever it wants). However from the native side it will be expected to exist. Calls going back (from native to managed) to the disposed instance will crash your application.

    The right pattern is to declare navigationController as a field (instead of a local variable) of your type and create/assign it in the method. This will keep a reference to navigationController alive as long as the parent instance exists (and ensure any callback won't go to a disposed object).

    E.g.

    private UINavigationController navigationController;
    ...
    public override void ViewDidLoad ()
    {
        ...
        var dialogViewController = new DialogViewController(rootElement);
        navigationController = new UINavigationController(dialogViewController);
    
        View.Add (navigationController.View);
        ...
    }
    

    0 讨论(0)
提交回复
热议问题