2016-03-07 69 views
3

我有一個 S IN根:如何更改與自定義渲染的TableSection文本顏色 - Xamarin.Forms C#

var tableView = new TableView 
{ 
    RowHeight = 60, 
    Root = new TableRoot 
    { 
    firstTableSection, 
    secondTableSection, 
    thirdTableSection, 

    } 
} 

var firstTableSection = new TableSection("First") 
{ 
    // Cells 
} 

var firstTableSection = new TableSection("First") 
{ 
    // Cells 
} 

var firstTableSection = new TableSection("First") 
{ 
    // Cells 
} 

你怎樣才能改變TableSection文本的文本顏色與自定義渲染器?

public class TestTableViewRenderer : Xamarin.Forms.Platform.Android.TableViewRenderer 
{ 
    protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Xamarin.Forms.TableView> e) 
    { 
     base.OnElementChanged(e); 

     // Not sure how to add this property here 

    } 
} 

回答

3

我結束了使用此解決方案:

https://forums.xamarin.com/discussion/32379/changing-the-title-color-of-a-tablesection

這依賴於兩個自定義渲染:

[assembly: ExportRenderer(typeof(TableView), typeof(CustomTableView))] 
namespace APP.Droid 
{ 
    public class CustomTableView : TableViewRenderer 
    { 
     protected override TableViewModelRenderer GetModelRenderer(global::Android.Widget.ListView listView, TableView view) 
     { 
      return new CustomTableViewModelRenderer(this.Context, listView, view); 
     } 

    } 
} 

and

[assembly: ExportRenderer(typeof(TableViewModelRenderer), typeof(CustomTableViewModelRenderer))] 
namespace APP.Droid 
{ 
    public class CustomTableViewModelRenderer : TableViewModelRenderer 
    { 
     public CustomTableViewModelRenderer(Context Context, global::Android.Widget.ListView ListView, TableView View) 
      : base(Context, ListView, View) 
     { } 
     public override global::Android.Views.View GetView(int position, global::Android.Views.View convertView, ViewGroup parent) 
     { 
      var view = base.GetView(position, convertView, parent); 

      var element = this.GetCellForPosition(position); 

      if (element.GetType() == typeof(TextCell)) 
      { 
       try 
       { 
        var text = ((((view as LinearLayout).GetChildAt(0) as LinearLayout).GetChildAt(1) as LinearLayout).GetChildAt(0) as TextView); 
        var divider = (view as LinearLayout).GetChildAt(1); 

        text.SetTextColor(Android.Graphics.Color.Rgb(50, 50, 50)); 
        divider.SetBackgroundColor(Android.Graphics.Color.Rgb(150, 150, 150)); 
       } 
       catch (Exception) { } 
      } 

      return view; 
     } 
    } 
} 
相關問題