<|start_filename|>src/xamformsdemo/xamformsdemo/CustomControls/ExtendedViewCell.cs<|end_filename|>
﻿using Xamarin.Forms;

namespace xamformsdemo.CustomControls
{
  /// <summary>
  /// Will be swapped out by a Custom Renderer which will override/reset 
  /// the background colour of the cell based on whether it's (un)selected
  /// </summary>
  public class ExtendedViewCell : ViewCell
  {
    /// <summary>
    /// The SelectedBackgroundColor property.
    /// </summary>
    public static readonly BindableProperty SelectedBackgroundColorProperty =
        BindableProperty.Create("SelectedBackgroundColor", typeof(Color), typeof(ExtendedViewCell), Color.Default);

    /// <summary>
    /// Gets or sets the SelectedBackgroundColor.
    /// </summary>
    public Color SelectedBackgroundColor
    {
      get { return (Color)GetValue(SelectedBackgroundColorProperty); }
      set { SetValue(SelectedBackgroundColorProperty, value); }
    }
  }
}

<|start_filename|>src/xamformsdemo/xamformsdemo.Droid/Effects/ChangeColorSwitchEffect.cs<|end_filename|>
﻿using Android.Graphics;
using Android.Support.V7.Widget;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Color = Xamarin.Forms.Color;
using Switch = Android.Widget.Switch;

using ChangeColorSwitchEffect = xamformsdemo.Droid.Effects.ChangeColorSwitchEffect;
using XECCSwitchEffect = xamformsdemo.Effects.ChangeColorSwitchEffect;

[assembly: ExportEffect(typeof(ChangeColorSwitchEffect), "ChangeColorSwitchEffect")]

namespace xamformsdemo.Droid.Effects
{
  /// <summary>
  /// Based on the samples at https://github.com/FormsCommunityToolkit/Effects
  /// and  http://stackoverflow.com/questions/11253512/change-on-color-of-a-switch
  /// </summary>
  [Android.Runtime.Preserve(AllMembers = true)]
  public class ChangeColorSwitchEffect : PlatformEffect
  {
    private Color _trueColor;
    private Color _falseColor;
    private Color _thumbColor;

    // slightly darker for the tracks, otherwise there's no virtual
    // 'depth' below the 'thumb' part of the slider.
    private Color _falseColorDarker;
    private Color _trueColorDarker;

    protected override void OnAttached()
    {
      if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBean)
      {
        _thumbColor = (Color)Element.GetValue(XECCSwitchEffect.ThumbColorProperty);
        _trueColor = (Color)Element.GetValue(XECCSwitchEffect.TrueColorProperty);
        _falseColor = (Color)Element.GetValue(XECCSwitchEffect.FalseColorProperty);

        _falseColorDarker = _falseColor.AddLuminosity(-0.25);
        _trueColorDarker = _trueColor.AddLuminosity(-0.25);

        ((SwitchCompat)Control).CheckedChange += OnCheckedChange;

        ((SwitchCompat)Control).TrackDrawable.SetColorFilter(_falseColorDarker.ToAndroid(), PorterDuff.Mode.Multiply);

        ((SwitchCompat)Control).ThumbDrawable.SetColorFilter(_thumbColor.ToAndroid(), PorterDuff.Mode.Multiply);
        // to change the colour of the thumb-drawable to the 'true' (or 'false') colour, enable the line below (and disable the one above)
        // ((SwitchCompat)Control).ThumbDrawable.SetColorFilter(_trueColor.ToAndroid(), PorterDuff.Mode.Multiply);
      }
    }

    private void OnCheckedChange(object sender, CompoundButton.CheckedChangeEventArgs checkedChangeEventArgs)
    {
      if (checkedChangeEventArgs.IsChecked)
      {
        ((SwitchCompat)Control).TrackDrawable.SetColorFilter(_trueColorDarker.ToAndroid(), PorterDuff.Mode.Multiply);
        // to change the colour of the thumb-drawable to the 'true' (or false) colour, enable the line below
        // ((SwitchCompat)Control).ThumbDrawable.SetColorFilter(_trueColor.ToAndroid(), PorterDuff.Mode.Multiply);
      }
      else
      {
        ((SwitchCompat)Control).TrackDrawable.SetColorFilter(_falseColorDarker.ToAndroid(), PorterDuff.Mode.Multiply);
        // to change the colour of the thumb-drawable to the 'true' (or false) colour, enable the line below
        // ((SwitchCompat)Control).ThumbDrawable.SetColorFilter(_thumbColor.ToAndroid(), PorterDuff.Mode.Multiply);
      }
    }

    protected override void OnDetached()
    {
      if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBean)
      {
        ((Switch)Control).CheckedChange -= OnCheckedChange;
      }
    }
  }
}

<|start_filename|>src/xamformsdemo/xamformsdemo/ListViewDemoPage.xaml.cs<|end_filename|>
﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Xamarin.Forms;

namespace xamformsdemo
{
  public partial class ListViewDemoPage : ContentPage
  {

    public ObservableCollection<MyModel> MyModelsList { get; set; }

    public ListViewDemoPage()
    {
      InitializeComponent();


      MyModelsList = new ObservableCollection<MyModel>(MakeAListOfMyModels());

      BindingContext = MyModelsList;
    }

    private List<MyModel> MakeAListOfMyModels()
    {
      var lst = new List<MyModel>();
      for (int i = 0; i < 20; i++)
      {
        lst.Add(new MyModel() {ItemName = $"This is Item {i:00}", LastUpdated = DateTime.Now.AddMinutes(-1 * i)});
      }
      return lst;
    }
  }

  public class MyModel
  {
    public string ItemName { get; set; }
    public DateTime LastUpdated { get; set; }
  }
}


<|start_filename|>src/xamformsdemo/xamformsdemo.iOS/CustomRenderers/ExtendedViewCellRenderer.cs<|end_filename|>
﻿using UIKit;
using xamformsdemo.CustomControls;
using xamformsdemo.iOS.CustomRenderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(ExtendedViewCell), typeof(ExtendedViewCellRenderer))]
namespace xamformsdemo.iOS.CustomRenderers
{
  public class ExtendedViewCellRenderer : ViewCellRenderer
  {
    public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
    {
      var cell = base.GetCell(item, reusableCell, tv);
      var view = item as ExtendedViewCell;
      cell.SelectedBackgroundView = new UIView
      {
        BackgroundColor = view.SelectedBackgroundColor.ToUIColor(),
      };

      return cell;
    }

  }
}


<|start_filename|>src/xamformsdemo/xamformsdemo/CustomControlsPage.xaml.cs<|end_filename|>
﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Xamarin.Forms;

namespace xamformsdemo
{
  public partial class CustomControlsPage : ContentPage
  {


    public CustomControlsPage()
    {
      InitializeComponent();
      //BindingContext = ViewModel;
    }

  }

}


<|start_filename|>src/xamformsdemo/xamformsdemo/MainPage.xaml.cs<|end_filename|>
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace xamformsdemo
{
  public partial class MainPage : ContentPage
  {
    public MainPage()
    {
      InitializeComponent();
    }

    private async void BtnListViewDemo_OnClicked(object sender, EventArgs e)
    {
      await this.Navigation.PushAsync(new ListViewDemoPage());
    }

    private async void BtnCustomControls_OnClicked(object sender, EventArgs e)
    {
      await this.Navigation.PushAsync(new CustomControlsPage());
    }
  }
}


<|start_filename|>src/xamformsdemo/xamformsdemo.iOS/Effects/ChangeColorSwitchEffect.cs<|end_filename|>
﻿using System;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

using ChangeColorSwitchEffect = xamformsdemo.iOS.Effects.ChangeColorSwitchEffect;
using XECCSwitchEffect = xamformsdemo.Effects.ChangeColorSwitchEffect;

[assembly: ExportEffect(typeof(ChangeColorSwitchEffect), "ChangeColorSwitchEffect")]

namespace xamformsdemo.iOS.Effects
{
  public class ChangeColorSwitchEffect :PlatformEffect
  {
    private Color _trueColor;
    private Color _falseColor;
    private Color _thumbColor;

    protected override void OnAttached()
    {
      _trueColor = (Color)Element.GetValue(XECCSwitchEffect.TrueColorProperty);
      _falseColor = (Color)Element.GetValue(XECCSwitchEffect.FalseColorProperty);
      _thumbColor = (Color)Element.GetValue(XECCSwitchEffect.ThumbColorProperty);

      var switchControl = Control as UISwitch;
      if (switchControl != null)
      {
        switchControl.TintColor = UIColor.FromRGBA((nfloat)_falseColor.R, (nfloat)_falseColor.G, (nfloat)_falseColor.B, 0.75f);
        // enable and fiddle with these to change initial track colour 
        // switchControl.Layer.CornerRadius = switchControl.Frame.Height / 2;
        // switchControl.BackgroundColor = switchControl.TintColor;

        switchControl.OnTintColor = UIColor.FromRGBA((nfloat)_trueColor.R, (nfloat)_trueColor.G, (nfloat)_trueColor.B, 0.75f);
        switchControl.ThumbTintColor = UIColor.FromRGBA((nfloat)_thumbColor.R, (nfloat)_thumbColor.G, (nfloat)_thumbColor.B, 1.0f);
      }
    }

    protected override void OnDetached()
    {

    }
  }
}