MonoTouch Exception not getting caught on device
Asked Answered
T

1

1

I am working on an app which was previously developed without using Storyboards, i.e. all the views were created in code. So as per this link, I am trying to pass a custom MvxTouchViewsContainer to MvxTouchSetup.CreateTouchViewsContainer.

protected override IMvxTouchView CreateViewOfType(Type viewType, MvxViewModelRequest request)
    {

        UIStoryboard storyboard;

        try
        {

            storyboard = UIStoryboard.FromName(viewType.Name, null);
        }
        catch (Exception)
        {
            try {
                storyboard = UIStoryboard.FromName("MainStoryBoard_iPhone", null);
            }
            catch (Exception) {
                return base.CreateViewOfType (viewType, request);
            }
        }

        IMvxTouchView resultView = null;
        try {
             resultView = (IMvxTouchView) storyboard.InstantiateViewController(viewType.Name);

        } catch(Exception) {
            resultView = base.CreateViewOfType (viewType, request);

        }
        return resultView;
    }

Basically I try to find a storyboard if present, else I fallback to loading the view from code. The try - catch exceptions works fine in the simulator. However, the exceptions are not getting caught when running on an actual iOS Device and the app is crashing.

I get a MonoTouch.Foundation.MonoTouchException which contains NSInvalidArgumentException which basically is expected because, some views may not have a storyboard associated with them.

Is this a Xamarin bug?

Taub answered 19/2, 2015 at 4:0 Comment(2)
I experienced something similar when using HockeyApp to log exceptions. Are you using some form of exception wrapping (Testflight e.g.)? Try commenting it out and see if that helps.Patton
I use a custom UnhandledExceptionEventArgs in my app. I commented it out but the issue still persists.Taub
P
3

Using a custom attribute works better than the try/catch-method:

public class FromStoryboardAttribute : Attribute
{
    public string StoryboardName { get; set; }

    public FromStoryboardAttribute(string storyboardName = null)
    {
        StoryboardName = storyboardName;
    }
}

Here is the StoryBoardContainer:

public class StoryBoardContainer : MvxTouchViewsContainer
{
    protected override IMvxTouchView CreateViewOfType(Type viewType, MvxViewModelRequest request)
    {
        var storyboardAttribute = viewType.GetCustomAttribute<FromStoryboardAttribute>();
        if (storyboardAttribute != null) {
            var storyboard = UIStoryboard.FromName(storyboardAttribute.StoryboardName ?? viewType.Name, null);
            var viewController = storyboard.InstantiateViewController(viewType.Name);
            return (IMvxTouchView) viewController;
        }
        return base.CreateViewOfType(viewType, request);
    }
}

Usage:

// Will look for a UIViewController with identifier "MyView" inside a Storyboard named "MyView.storyboard"
[FromStoryboard]
public class MyView : MvxViewController
{
    public MyView(IntPtr handle) : base(handle) {}
}

// Will look for a UIVIewController with identifier "MyOtherViewInSameStoryboard" inside a Storyboard named "MyView.storyboard"
[FromStoryboard(StoryboardName = "MyView")]
public class MyOtherViewInSameStoryboard : MvxViewController
{
    public MyOtherViewInSameStoryboard(IntPtr handle) : base(handle) {}
}
Patton answered 20/2, 2015 at 8:41 Comment(1)
Thanks a lot for this. This is a very elegant solution. I was looking for something along these lines - something like adding custom attrs for a VC in the storyboard . Unfortunately I am a newbie in C# .Taub

© 2022 - 2024 — McMap. All rights reserved.