Showing posts with label Xaml. Show all posts
Showing posts with label Xaml. Show all posts

Wednesday, January 25, 2012

Era of the XAML user interface

I hear the term XAML user interfaces mentioned way too often these days.

XAML has in fact nothing to do with userinterfaces. XAML is just a language for instantiating an object structure. I have previously written about how I have used it as a configuration language for my applications, a sort of light weight dependency injection framework. To show that WPF and Silverligth are not the only UI frameworks that can be used with XAML I have used XAML to create a small Windows Forms form.

The root node of my XAML file will be Form. By providing "clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" as the default namespace Visual Studio and .net will recognize the elements and validate them to match a structure that is allowed beneath a form. If you need to use other .net namespaces, you just add a prefixed xml namespace for each of those .net namespaces.




Visual Studio will pr default open a WPF/Silverlight visual presentation along with the text editor when opening the XAML file. As we are not using WPF or silverlight the visual presentation will not show anything usefull. The XAML can be distributed as a file besides you application, embedded as XAML or embedded as BAML (XAML compiled to a binary format). The BAML approach is probably the most sensible solution for a real life application.

To create an instance of the form just give the XAML stream to a XamlReader and cast the result to a Form object. Now the form can be displayed by calling Show, like you would on any other windows form.


When the form object has been created, i use two lines of code to hookup some eventhandlers before showing the form. The find method on the form is an extension method for Form that flattens that flattens the structure of the form so that LINQ can be used on it. The generic type is a filter that only returns the controls of that type and the lambda is a further filter. The find method has been implemented in the plain framework, so you can go there to se the implementation.
The Do method is just like the ForEach method on List, this however work for any type of IEnumerable<T>.

The first of the two lines finds all buttons whose name start with "Command" and hook them up to an event handler. The second line hooks an event handler to the TextChanged event of all controls whose name start with Value. In this way it is easy to use a naming convention to hook the UI up to business logic. Dependind on your architecture, you might hook it up to fire commands or whatever.

You might implement some layout manager the same way, or you might inherit panel or something other control and create a layout manager that way. By implementing you own controls you can also put metadata on the controls in your UI.

So, XAML has the potential of being used with other UI frameworkds than WPF and Silverlight.

http://dotnetexception.blogspot.com/2008/04/xaml-ultra-lightweight-ioc-container.html

Monday, June 09, 2008

Real Life Dependency Injection

When making a framework you come across the question of how to instantiate your objects, and how the users of your framework can inject object in the framework. Often you want it to be flexible enough for it to be easy to inject object into, but fluent enough for the programmers not to notice the low dependency barrier.

What we did was to make a static application class with a reference to a configuration object which is only loaded once at application start. The application class initiates the configuration object by running a xaml file through the xaml parser. This is a very pragmatic solution that can be used for initialization of static objects in the application. Everything is hooked together with very little code and it can easily be extended to support more objects etc. in applications using the framework.

When the user needs access to any of the injected object he can do like this:

Application.Config.DaoFactory


Although this only supports "singleton" objects it gives me the flexibility I was looking for.

Sunday, April 06, 2008

Xaml, the ultra lightweight IOC container

I've been testing xaml for defining what object to create at runtime. Although xaml is often linked to windows workflow foundation or windows presentation foundation, it can easily be applied to your own domain.

Xaml is a xml language where you can define how a .net object structure is to be initialized. It has many similarities to springframework.net, lightweight IOC.

The greatest thing about Xaml is it's very easy syntax. Every node in your xml is giveng the name of the class to instantiate. By givin the root node a namespace, your VS.Net provides full intellisense to what properties are on what classes and what type they are. By making typeconverters for your classes you are able to provide a simple way of creating your simple classes.

Missing?
What is missing then? Well... Xaml is just a language that enables you to create objects. Because of this it doesn't give you the means of controlling the lifespan of your objects. You can't state that an object should be a singleton for the application etc. If you want that you have to code it yourselfes.

Xaml supports property injection but not constructor injection which means all properties needs a public setter for it to be set by Xaml. Most of my properties already have public setters, but I have to alter a few for them to be xaml enabled.

Another thing I'm missing is the support for generic types. In xaml you cannot instansiate List as an example. In stead you kan implement some xaml interfaces or you will have to make new classes that inherrit from the generic type you want.

Example

I have used my search criteria framework that I introduced in "A use for operator overloading" for testing the Xaml.



The above xaml creates the object structure ressembling the following query: Content = "teste feste" or Title = "teste feste" or (Content="test" and Title="test")

I would also have liked a possibility to cross reference objects (create it once and reuse it multiple places in my xaml), but overall xaml provides a very simple way of creating your object away from your code. Whereas spring.net has quite a heavy syntax xaml is very intuitive and will be comprehendable, even for people with no technical background.