如何在DialogViewController上将背景设置为透明

当我将MonoTouch.Dialog背景颜色设置为uiclear(透明)时,它会抛出exception,为什么? 以及如何将其设置为透明。

未处理的exception:System.NullReferenceException:未将对象引用设置为MyDialogViewController.LoadView()中MyDialogViewController.cs中的[0x00016]对象引用: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 最有可能发生,因为ParentViewControllernull

根据你的MyDialogViewController的显示方式,这可能是由于使用了错误的属性和最近的iOS5 引起的 :

在iOS 5.0之前,如果视图没有父视图控制器并且正在呈现,则将返回呈现视图控制器。 在iOS 5上,此行为不再发生。 而是使用presentsViewController属性来访问呈现视图控制器。

但是,如果MyDialogViewController是窗口的RootViewController那么这些属性为null是正常的。 在这种情况下,简单地使用TableView上的UIColor.Clear我一个黑色背景(我什么也没有),所以它应该足够MT.D部分。 如果你有一个父母,那么你可以尝试在显示你的MyDialogViewController之前设置它的背景颜色来清除(如果需要的话)。