【问题标题】:Is there a way to set the BackgroundColor for a RadioGroup?有没有办法为 RadioGroup 设置 BackgroundColor?
【发布时间】:2012-05-10 17:49:58
【问题描述】:

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

        // 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 的样式(或至少设置背景颜色)?

【问题讨论】:

    标签: ios xamarin.ios monotouch.dialog


    【解决方案1】:

    在问简单的问题之前,我需要更多地使用程序集浏览器:-)

    幸运的是,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);
        }
    }
    

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

    【讨论】:

      【解决方案2】:

      为了让它工作,我必须重写 MakeViewController 方法并将通常返回的 UIViewController 转换为 UITableViewController,然后进行编辑。

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

      【讨论】:

        猜你喜欢
        • 2015-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-17
        相关资源
        最近更新 更多