2012-05-10 172 views
2

我正在使用MonoTouch.Dialog創建類似設置的頁面。下面的linq創建一組RootElement,每個RootElements都有一個具有一組RadioEventElements(爲創建OnSelected事件而創建的RadioElement的子類)的部分。有沒有辦法爲RadioGroup設置BackgroundColor?

 // 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屬性設置爲所需的顏色,我可以正確設置每個頁面的樣式......除了在導航到收音機組時由父DialogViewController自動創建並推送的新DialogViewController。

是否有任何方式來(或至少設置背景顏色)這個孩子DialogViewController?

回答

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

爲了得到這個工作,我不得不重寫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; 
} 
相關問題