Tuesday, October 28, 2014

Xamarin.Forms: Chainable and Strongly-Typed Binding Extension

In Xamarin.Forms, sometimes I like to create my bindings inline with the creation of my controls, as opposed to creating the controls, storing them, then set all bindings at the bottom. So here's an extension method, that adds chaining.

public static class BindableObjectExtensions
    // chainable, with a string for property
    // usage : new Button().WithBinding(Button.CommandProperty, "SomeCommand"),
    public static T WithBinding<T>(this T obj, 
        BindableProperty bindableProperty, 
        string path, 
        BindingMode mode = BindingMode.Default, 
        IValueConverter converter = null, 
        object converterParameter = null, 
        string stringFormat = null
        where T:BindableObject
        obj.SetBinding (bindableProperty, new Binding (path, mode, converter, converterParameter, stringFormat));
        return obj;

    // chainable, with a lambda for property
    // usage : new Button().WithBinding(Button.CommandProperty, (MyViewModel vm) => vm.SomeCommand),
    public static TOUT WithBinding<TOUT, TVM, TO>(this TOUT obj, 
        BindableProperty bindableProperty, 
        Expression<Func<TVM, TO>> sourceProperty, 
        BindingMode mode = BindingMode.Default, 
        IValueConverter converter = null, 
        object converterParameter = null, 
        string stringFormat = null
        where TOUT:BindableObject
        where TVM:INotifyPropertyChanged
        obj.SetBinding (bindableProperty, new Binding (((MemberExpression)sourceProperty.Body).Member.Name, mode, converter, converterParameter, stringFormat));
        return obj;

Usage :

new StackLayout{
    Children = 
        new Button().WithBinding(Button.CommandProperty, (MyViewModel vm) => vm.SomeCommand),

Wednesday, August 11, 2010


Here is an extension that helps with bitwise operations on (flagged) enums.

public enum Positions
None = 0, Left = 1, Right = 2, Top = 4, Bottom = 8,}

public static class EnumerationExtensions
{ public static T Append<T>(this Enum type, T value)
{ return (T)(object)(((int)(object)type | (int)(object)value)); } public static T Remove<T>(this Enum type, T value)
{ return (T)(object)(((int)(object)type & ~(int)(object)value)); } public static bool Contains<T>(this Enum type, T value)
{ return (((int)(object)type & (int)(object)value) == (int)(object)value); } public static bool ContainsAny<T>(this Enum type, T value)
{ return (((int)(object)type & (int)(object)value) != 0); } public static bool ContainsNot<T>(this Enum type, T value)
{ return !Contains(type, value); } public static bool Is<T>(this Enum type, T value)
{ return (((int)(object)type == (int)(object)value)); } }

then you'll be able to write tests like this :

Positions twoPositions = Positions.Left | Position.Top;
Console.WriteLine(twoPositions.Contains(Position.Top)) // true
Positions position = Positions.None;
Console.WriteLine(position.Is(Position.Bottom)); // true (None is overwritten)

Sunday, January 10, 2010

Comparing (Open)Office documents versions using TortoiseSVN !

Call me a noob for not knowing this already, but I just discovered a ‘prittttty’ useful trick for comparing Office (/ OpenOffice) documents versions. (Windows boxes only sorry)
Cool thing is that Word and Excel 2007 support documents comparisons / reviewing.
And called with the right arguments, reviewing can be automated using external tools.

See, as I’ve got my technical documents versioned by SVN along with my code files, I discovered TortoiseSVN is capable of launching different Diff/Merge tools based on the file extension.
You will only need to provide it with some script files that are not currently packaged with the Tortoise setup.

diff-scripts on github

Simply put them into the Diff-Scripts folder of your tortoise setup

“C:\Program Files\TortoiseSVN\Diff-Scripts\”

Then you can right-click on your .docx / .xlsx / .ppt / .od* / .sxw document that are under revisioning control and needs to be reviewed/committed and simply click ‘Diff’ in the TortoiseSVN right-click menu.

It will open the corresponding software in review mode.

Have a look at what it looks like in Word :

neat !

Sunday, August 30, 2009

10 Things Every Senior Flash Developer Should Know

10 Things Every Senior Flash Developer Should Know: "I’ve interviewed quite a few Flash/Flex developers as potential employees for Roundarch. The hard part for me is knowing exactly what questions to ask to be able to gauge a Flash dev’s skill level.
So, I made a list. From my experience this list allows me to judge the skill level and experience of the devs [...]"