MonoTouch 异常未在设备上捕获
MonoTouch Exception not getting caught on device
我正在开发一个应用程序,该应用程序以前是在不使用故事板的情况下开发的,即所有视图都是在代码中创建的。因此,根据 this link,我正在尝试将自定义 MvxTouchViewsContainer
传递给 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 设备上并且应用程序崩溃时,不会捕获异常。
我得到一个 MonoTouch.Foundation.MonoTouchException,其中包含 NSInvalidArgumentException,这基本上是预期的,因为某些视图可能没有与之关联的故事板。
这是 Xamarin 错误吗?
使用自定义属性比 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) {}
}
我正在开发一个应用程序,该应用程序以前是在不使用故事板的情况下开发的,即所有视图都是在代码中创建的。因此,根据 this link,我正在尝试将自定义 MvxTouchViewsContainer
传递给 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 设备上并且应用程序崩溃时,不会捕获异常。
我得到一个 MonoTouch.Foundation.MonoTouchException,其中包含 NSInvalidArgumentException,这基本上是预期的,因为某些视图可能没有与之关联的故事板。
这是 Xamarin 错误吗?
使用自定义属性比 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) {}
}