问题描述
当我将 MonoTouch.Dialog 背景色设置为 uiclear(transparent) 时,它抛出异常,为什么?以及如何将其设置为透明.
When I set the MonoTouch.Dialog background color to uiclear(transparent), it throw an exception, why? and How to set it to transparent.
未处理的异常:System.NullReferenceException:对象引用未设置到 MyDialogViewController.LoadView () [0x00016] in MyDialogViewController.cs 中的对象实例:ParentViewController.View.BackgroundColor = UIColor.Clear
public class MyDialogViewController: DialogViewController
{
public MyDialogViewController (RootElement root) : base (root)
{
}
public override void LoadView()
{
base.LoadView ();
this.TableView.BackgroundColor = UIColor.Clear;
ParentViewController.View.BackgroundColor = UIColor.Clear;
}
}
public void xxxxx(){
var menu = new RootElement(""){
new Section ("Demo"){
new EntryElement("Name", "",""),
},
};
var menuDVC = new MyDialogViewController (menu) {
Autorotate = true
};
this.View.AddSubview(menuDVC.View);
}
推荐答案
NullReferenceException
很可能发生,因为 ParentViewController
是 null代码>.
The NullReferenceException
most likely occurs because ParentViewController
is null
.
根据您的 MyDialogViewController
如何显示,这可能是由于使用了错误的属性和最近的 iOS5,更改:
Depending on how your MyDialogViewController
is showed this might be due to using the wrong property and a recent, iOS5, change:
在 iOS 5.0 之前,如果视图没有父视图控制器并且正在呈现,则将返回呈现视图控制器.在 iOS 5 上,此行为不再发生.相反,使用presentingViewController 属性来访问呈现视图控制器.
但是,如果 MyDialogViewController
是窗口的 RootViewController
,那么这些属性为 null
是正常的.在这种情况下,只需在 TableView
上使用 UIColor.Clear
即可获得黑色背景(我在那里什么都没有),因此它对于 MT.D 部分应该足够了.如果您有父级,则可以在显示 MyDialogViewController
之前尝试将其背景颜色设置为清除(如果需要).
However if the MyDialogViewController
is the window's RootViewController
then it's normal for those properties to be null
. In this case simply using UIColor.Clear
on the TableView
get me a black background (I had nothing there) so it should be enough for MT.D part. If you have a parent then you can try to set it's background color to clear (if needed) before displaying your MyDialogViewController
.
这篇关于如何在 DialogViewController 上将背景设置为透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!