Welcome to MSDN Blogs Sign in | Join | Help

saurabhd's musings

Saurabh Dasgupta works for Microsoft Consulting Services. - '640kb ought to be enough for anybody'
Dynamic loading of XAML in Silverlight- Interesting possibilities for Line of Business Applications

Introduction

Large scale Line of Business Applications (such as ERP, CRM, Accounting) require extensive end user customization. This is a natural outcome of the fact that no two business are alike and customizations are inevitable. Consider an Human Resource module of an ERP system. There would be differences in data attributes required for an Employee record from one organization to another.

The challenge lies in building a software framework that allows customization after the core product has been shipped. You do not want to maintain source code versions for each customer you sell your Accounting package to. In the past various ERP vendors have addressed the above problem by using home grown solutions. E.g. One of the largest ERP products stores the meta data of each and every UI widget for every form as distinct records of information in a configuration database. E.g. Width, Height, X, Y, Caption , etc. The user interface of the form is constrcuted on the fly at runtime.  Customizations at the end user level can be carried out by editing the configuration database.

XAML

XAML is a very rich declarative language that rescues the modern day application developer from worrying about how to provide a dynamic UI engine. XAML is a general purpose declarative XML driven system to represent an Object hierarchy. Any object hierarchy of CLR compliant objects could be persisted in XAML syntax into a file or a BLOB in a database. The same object tree can be re-created from the XAML.

Dynamically instantiating objects in an external XAML file

In the sample that accompanies this blog, I have used the following syntax for loading a XAML file from the local disk and injecting the newly created tree into an existing Canvas element. Have reduced the code and XAML for the sake of simplicity.

Sample Xaml

                <Canvas>

                    <Canvas x:Name="xamlcontainer1" Width="300" Height="300"  Canvas.Left="20" Canvas.Top="60" Background="LightGray">
                        <TextBlock TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="msglog"> Region for dnyamically adding XAML</TextBlock>
                    </Canvas>
                </Canvas>

Sample Code

                Object dynaxaml;
                try
                {
                    xamlcontainer1.Children.Clear();  //clear the Canvas of any prior elements . such as the TextBlock
                    dynaxaml = XamlReader.Load(<XAML contents, passed as a string>);  //create the Object graph from the XAML string
                    xamlcontainer1.Children.Add((UIElement)dynaxaml) //inject the Object graph into the Canvas

                }


 

Note the presence of the  <Canvas x:Name='xamlcontainer1' > . The object graph is added to the Children property of the Canvas

Demo Silverlight Application

  1. Extract the contents of the attachments and save them to local disk
  2. The attachment contains sample code and two files DynamicUISample01.xaml and DynamicUISample02.xaml
  3. Select the button to browse for file DynamicUISample01.xaml on the local disk
  4. Click the Clear button to clear the contents of the Canvas where dynamically created objects were inserted.
  5. Repeat the process with DynamicUISample02.xaml

The Road Ahead

We are not done yet. The sample presented here establishes one of the very critical elements of building a class of LOB applications which require customization by the the end user (the IT team of the business owner to be precise). However, a complex application would require interaction with various serivces such as searching for records, displaying search records in a grid, navigating to and fro in the search results, deleting a record and so on. Principles layed down by Composite Application Block for WPF would have to be harnessed to build applications which would work in a plug anf play fashion.

Windows SharePoint Services 3.0 document library as a repository for XAML files!

I see immense potential for leveraging WSS as a repository for dynamic screen definitions. As somebody who has worked with MOSS from the time it was in Beta 1, I find it difficult to rationalize building custom databases for such a purpose. Shall blog about this some other day. Tata for now.

Posted: Tuesday, November 25, 2008 11:10 PM by Saurabh Dasgupta

Attachment(s): DynamicLoading.zip

Comments

Nigel M said:

Is there a way to insert a child element from one XAML file programtically into another.

ie. I have one XAML page which is the base template for a site.  Then I have a second which contains the content.

I want to insert a specific child of the content into a child of the base template.

I have tried doing this, but get an error saying that the element I am trying to add already has a parent.

As a work-around I have a seperate XAML file for each piece of content and just insert the whole thing.

Any ideas?

# November 25, 2008 10:30 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker