ObjectFactory feature complete
As of changeset 21175, the ObjectFactory is feature complete. We now
have the same kind of features as XAML has. In this blog entry, I'll
show some pseudo code, which is possible now.
Normal objects
First have a look at the following code snippet:.... <!-- define the Object Types --> <Path name="/ObjectFactory/ObjectTypes"> <Type id="MyButton" class="NApf.BlogSamples.MyButtonPropertyContainer"></Type> <!-- other object types --> </Path> <!-- define the Object Creators --> <Path name="/ObjectFactory/ObjectCreators"> <Type id="MyButton" class="NApf.BlogSamples.MyButtonObjectCreator"></Type> <!-- other object creators --> </Path> <Path name="/MyPath"> <ObjectFactory id="Button1" type="MyButton" label="Button 1" ...other button properties...></ObjectFactory> ...Other object definitions... </Path>
The ObjectFactory has a layered design, to allow more flexibility.
When a given AddInTreePath is read, PropertyContainers are created. These PropertyContainers are meant to be generic. You are not supposed to have multiple implementations for one object type.
Next step in the process is lookup the ObjectCreator for each PropertyContainer, and have the ObjectCreator create the actual object. Take a look at the code snippet again. The ObjectCreator MyButtonObjectCreator will, depending on the frontend active, create a native WPF button, a native WinForms button, or even a GTK# button.
Extended Properties
ObjectFactory's Extended Properties are much like the Attached Properties of XAML. See the snippet below(The ObjectTypes and ObjectCreators definitions are omitted for focusing. The ToolButton definitions would normally have a Label property, and others, but these are also omitted ):<Path name="/MyMainForm"> <ObjectFactory id="ToolBar" type="MyToolBar"> <ObjectFactory id="LoadButton" type="MyToolButton" MyToolBar.Position="2"/> <ObjectFactory id="NewButton" type="MyToolButton" MyToolBar.Position="1"/> </ObjectFactory> </Path>The above sample would provide you with a toolbar which has 2 buttons, which are, from left to right, the NewButton and the LoadButton.
Inheritance support
The following sample shows how inheritance works. Note that this is a very basic sample. It defines a derived toolbar, which has the above toolbar as base. Again, some property definitions are omitted.<Path name="/MyMainForm"> <ObjectFactory id="DerivedToolBar" type="$Inherit" base="/MyMainForm/ToolBar"> <ObjectFactory id="SaveButton" type="MyToolButton" MyToolBar.Position="3"/> </ObjectFactory> </Path>As you can see, it's quite easy to use the inheritance feature of the ObjectFactory.
Value Expressions
ObjectFactory's Value Expressions are equal in purpose as the Markup Extensions in XAML. They let you insert "complex" values into properties more easily (other possible purposes possible as well). Nested Value Expressions are supported too (using the result of a Value Expression as parameter of another Value Expression):<Path name="/MyMainForm">
<ObjectFactory id="StatusBar" type="MyStatusBar">
<ObjectFactory id="StaticMessage" type="MyStatusPane">
<ObjectFactory type="$Property" name="Message"
value="{ConcatString string1={StringParser string='User'} string2='@' string3={StringParser string='Platform'}}"/>
</ObjectFactory>
</ObjectFactory>
</Path>
The sample also shows a different way for setting properties. This is
especially handy in combination with conditions. In the property value, there are 2 different Value Expressions used. The ConcatString Value Expression concatenates all passed strings in their occurring order. The StringParser Value Expression returns the result of the StringParser.Parse method, allowing translation of properties using the ResourceService, by utilizing the StringParser.
What's next
One of the next things we will be working on is creating a extensive set of controls, which lets you easily create extensible, crossplatform User interfaces using the ObjectFactory. For now we will focus on WinForms and WPF support, but GTK# will be added later on.
