有没有办法为RadioGroup设置背景颜色?

时间:2012-05-10 17:49:58

标签: ios xamarin.ios monotouch.dialog

我正在使用MonoTouch.Dialog创建类似设置的页面。下面的linq创建了一组RootElements,每个RootElements都有一个部分,其中包含一组RadioEventElements(我创建的RadioElement的子类,用于添加OnSelected事件)。

        // initialize other phone settings by creating a radio element list for each phone setting
        var elements = (from ps in PhoneSettings.Settings.Keys select (Element) new RootElement(ps, new RadioGroup(null, 0))).ToList();

        // loop through the root elements we just created and create their substructure
        foreach (RootElement rootElement in elements)
        {
            rootElement.Add(new Section()
            {
                (from val in PhoneSettings.Settings[rootElement.Caption].Values select (Element) new RadioEventElement(val.Name)).ToList()
            });
            // ...
        }       

我实现的其中一个设置是“主题” - 目前它只是应用程序中各种屏幕的背景颜色。我可以通过将TableView.BackgroundColor属性设置为所需的颜色来正确地设置每个页面的样式...除了新的DialogViewControllers,当它导航到一个无线电组时由父DialogViewController自动创建和推送。

有没有办法设置这个子DialogViewController的样式(或至少设置背景颜色)?

2 个答案:

答案 0 :(得分:4)

在提出简单问题之前,我需要更多地使用汇编浏览器: - )

幸运的是,RootElement有一个名为PrepareDialogViewController的虚方法,看起来就是这个目的。我所要做的就是创建RootElement的一个简单子类并覆盖此方法以获得我想要的行为。

public class ThemedRootElement : RootElement 
{    
    public ThemedRootElement(string caption) : base (caption)
    {
    }

    public ThemedRootElement(string caption, Func<RootElement, UIViewController> createOnSelected) : base (caption, createOnSelected)
    {
    }

    public ThemedRootElement(string caption, int section, int element) : base (caption, section, element)
    {
    }

    public ThemedRootElement(string caption, Group group) : base (caption, group)
    {
    }

    protected override void PrepareDialogViewController(UIViewController dvc)
    {
        dvc.View.BackgroundColor = UIColorHelper.FromString(App.ViewModel.Theme.PageBackground);
        base.PrepareDialogViewController(dvc);
    }
}

希望这有助于在那里节省一些时间......

答案 1 :(得分:1)

为了使其工作,我必须覆盖MakeViewController方法并强制转换它通常返回到UITableViewController的UIViewController,然后进行编辑。

protected override UIViewController MakeViewController()
{
    var vc = (UITableViewController) base.MakeViewController();

    vc.TableView.BackgroundView = null;
    vc.View.BackgroundColor = UIColor.Red; //or whatever color you like
    return vc;
}