c# - Creating .NET equivalent Colors static class in Mono Android (Xamarin.Android) -


a lot of canvas methods in android api require paint object defined in order define color. method doing is,

paint mypaintobject = new paint(); mypaintobject.color = color.red; canvas.drawrect(..., mypaintobject); 

it better if looked this,

canvas.drawrect(..., colors.red); 

a solution class might this...

public static class colors {     public static paint red { { return getcolors(color.red); } }     public static paint black { { return getcolors(color.black); } }      private static paint getcolors(color color)     {         paint paint = new paint ();         paint.color = color;         return paint;     } } 

but suck have create getters every color available. ideas making easier?

edit: linq pretty solution this. per @chrissinclair's comment regarding populate list solidcolorbrush brushes

this.colors = typeof(color)     .getproperties(system.reflection.bindingflags.static |                     system.reflection.bindingflags.public)         .todictionary(p => p.name,                        p => new paint()                        { color = ((color)p.getvalue(null, null)) }); 

when called, looks like,

canvas.drawrect(..., colors["red"]); 

i recommend extension method convert color paint:

 public static paint aspaint(this color color)  {     paint paint = new paint ();     paint.color = color;     return paint;            } 

this allow write, color:

canvas.drawrect(..., color.red.aspaint()); 

one advantage here you're not hiding fact you're creating paint instance each time. using colors.red suggests you're creating color, not paint, , masks it's being constructed each call.

otherwise, if wish make colors class each property, you'll need property per color support. done scripting creation of source file, there no direct way create of these "colors" without writing property per color.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -