MonoTouch异常未在设备上被捕获 [英] MonoTouch Exception not getting caught on device

查看:85
本文介绍了MonoTouch异常未在设备上被捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个以前未使用情节提要开发的应用程序,即所有视图都是在代码中创建的.因此,按照链接,我正在尝试传递自定义MvxTouchViewsContainerMvxTouchSetup.CreateTouchViewsContainer.

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;
    }

基本上,我会尝试找到一个情节提要(如果存在),否则我会回退以从代码加载视图. try-catch异常在模拟器中可以正常工作.但是,在实际的iOS设备上运行时,未捕获到异常,并且应用程序崩溃了.

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.

我得到一个包含NSInvalidArgumentException的MonoTouch.Foundation.MonoTouchException,这基本上是可以预期的,因为某些视图可能没有与之关联的故事板.

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

这是Xamarin的错误吗?

Is this a Xamarin bug?

推荐答案

使用自定义属性要比try/catch-method更好:

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;
    }
}

这是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);
    }
}

用法:

// 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) {}
}

这篇关于MonoTouch异常未在设备上被捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆