5 Feb
2012

NuGet Project Uncovered: DynamicXaml

Category:UncategorizedTag: :

If you are coming to this series of posts for the first time you might check out my introductory post for a little context.

DynamicXaml takes a fluent approach to building up xaml in code. Following the pattern laid down by the HtmlTags project, this project provides a very clean way to write out your xaml code in C#.

I?ve copied a sample from the github readme below.

var _builder = new XamBuilder();
var button = 
  _builder.Start<Button>()
    .Margin("5,0")
    .WidthAndHeight(200d,30d)
    .Create();

One of the things I like about these projects is if you can?t use markup and are pushed to write your xaml in C#, you can apply some great tricks to reduce duplication with your code. (Not sure how this library works out, but I would suspect that if you created a default Button object, it would allow you to override specific properties one at a time or extend with new properties. This way you can create your base controls and only tweak as necessary.

2 thoughts on “NuGet Project Uncovered: DynamicXaml

  1. Would be even nicer if they had expression overloads for stuff like BindContent. Then you could write BindConten(b => b.Text)

    Daniel

  2. Hello Daniel,
    as creator of this still quite pre 1.0 package, I would like to chime in: You can write BindContent(“Text”), the darned thing about using dynamic in this context is that the whole lambda resolution business is done by the compiler. Inference of delegate type for lambdas breaks down in the dynamic scenario, so you have to specify the type of your lambda expression yourself, which is why the property of the DataContext to be bound is specified as a string. 

    For this the project has some helpers that set the correct type for some operations, but by thinking about your suggestion a new helper should be possible that will allow you to do this:

    // This op sets bind to a fixed, known type;
    var bind = X.ContextBinder();
    // bind is some func type whose return is understood by DynamicXaml
    _builder.Start().BindContent(bind(x => x.Text));

    I will see if I can implement it in the near future.

Comments are closed.