Skip to content

Instantly share code, notes, and snippets.

@prashantvc
Created January 20, 2015 11:52
Show Gist options
  • Save prashantvc/c19bfebf84e882aeedc0 to your computer and use it in GitHub Desktop.
Save prashantvc/c19bfebf84e882aeedc0 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Xamarin.Forms;
using System.ComponentModel;
namespace ListBinding
{
public class NamedColor : INotifyPropertyChanged
{
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName){
if (PropertyChanged != null) {
PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
}
}
#endregion
// Instance members.
public NamedColor()
{
}
public string Name { set; get; }
public string FriendlyName { set; get; }
public Color Color { set; get; }
// Static members.
static NamedColor()
{
List<NamedColor> all = new List<NamedColor>();
StringBuilder stringBuilder = new StringBuilder();
// Loop through the public static fields of type Color.
foreach (FieldInfo fieldInfo in
typeof (NamedColor).GetRuntimeFields ())
{
if (fieldInfo.IsPublic &&
fieldInfo.IsStatic &&
fieldInfo.FieldType == typeof (Color))
{
// Convert the name to a friendly name.
string name = fieldInfo.Name;
stringBuilder.Clear();
int index = 0;
foreach (char ch in name)
{
if (index != 0 && Char.IsUpper(ch))
{
stringBuilder.Append(' ');
}
stringBuilder.Append(ch);
index++;
}
// Instantiate a NamedColor object.
NamedColor namedColor = new NamedColor
{
Name = name,
FriendlyName = stringBuilder.ToString(),
Color = (Color)fieldInfo.GetValue(null)
};
// Add it to the collection.
all.Add(namedColor);
}
}
all.TrimExcess();
All = all;
}
public void RemoveItemAt(int index){
All.RemoveAt (index);
RaisePropertyChanged ("All");
System.Collections.ObjectModel.ObservableCollection<NamedColor> a;
}
//public static IEnumerable<NamedColor> All { set; get; }
public static List<NamedColor> All { set; get; }
// Color names and definitions from http://www.w3.org/TR/css3-color/
// (but with color names converted to camel case).
public static readonly Color AliceBlue = Color.FromRgb(240, 248, 255);
public static readonly Color AntiqueWhite = Color.FromRgb(250, 235, 215);
public static readonly Color Aqua = Color.FromRgb(0, 255, 255);
public static readonly Color Aquamarine = Color.FromRgb(127, 255, 212);
public static readonly Color Azure = Color.FromRgb(240, 255, 255);
public static readonly Color Beige = Color.FromRgb(245, 245, 220);
public static readonly Color Bisque = Color.FromRgb(255, 228, 196);
public static readonly Color Black = Color.FromRgb(0, 0, 0);
public static readonly Color BlanchedAlmond = Color.FromRgb(255, 235, 205);
public static readonly Color Blue = Color.FromRgb(0, 0, 255);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment