Other Recent Articles

Run-time XAML code compiler

By Eranda Ketawalage on Sunday, November 6, 2011 0 comments


XAML(Extensible Application Markup Language) is a declarative markup language. As applied to the .NET Framework programming model, XAML simplifies creating a UI for a .NET Framework application. You can create visible UI elements in the declarative XAML markup, and then separate the UI definition from the run-time logic by using code-behind files, joined to the markup through partial class definitions.

In both WPF and Silverlight interfaces designing use XAML.


In this post I'll show how to execute and display user entered XAML code at the runtime.



  • first you have to create a silverlight application
  • add a text box to take the user inputs (editingTxt)
  • add a text box to display error messages (statusTextBlock)
  • add a border controller to display the output (output)
  • add a button control and put the following code in to button click event

            object obj;
            try
            {
                obj = XamlReader.Load(editingTxt.text);
            }
            catch (XamlParseException exc)
            {
                statusTextBlock.Text = exc.Message.ToString();
                string ccode = str;
                string[] lines = Regex.Split(ccode, "\r\n");
                int lineCount = lines.Length;
                statusTextBlock.Foreground = errorBrush;
                editingTxt.Foreground = errorBrush;
                return;
            }

            if (obj == null)
            {
                statusTextBlock.Text = "NULL object";
                editingTxt.Foreground = errorBrush;
                return;
            }

            statusTextBlock.Text = "OK";
            editingTxt.Foreground = okBrush;

            if (obj is UIElement)
            {
                output.Child = obj as UIElement;
            }
            else
            {
                TextBlock txtblk = new TextBlock();
                txtblk.Text = obj.GetType().ToString();
                output.Child = txtblk;
            }



Demo Download :  http://www.sendspace.com/file/xxbfb6

Category:

0 comments:

Post a Comment

Amazon

Share this