Xamlreader. Load (): build up your own xamlpad

Source: Internet
Author: User
If you have been dabbling with any CTP release of winfx SDK, you probably have been irritated by the unstability of the xamlpad.exe which is shipped with the SDK. the xamlpad.exe crashes regularly on my machine because of some unhandled peculiar exceptions. so I have long been planning to build up my own xamlpad.
When I first encounter the xamlreader class which is in the system. windows. serialization namespace, I begin to realize that this class and the powerful methods it provides can serve me pretty well in designing my own xampad, the trick here is that the xamlreader class has a method called load which can take some arbitrary well formed XAML text as input, and produces corresponding WPF control as output, the signature of this method is:

Public static object load (xmlreader reader)

So first off, you have to construct an xmlreader object based on The XAML text input, and pass it to the xamlreader. load method as an argument:

Stringreader = NULL;
Xmltextreader xmlreader = NULL;
Try
{
Stringreader = new stringreader (xamldocument );
Xmlreader = new xmltextreader (stringreader );
Uielement DocumentRoot = (uielement) xamlreader. Load (xmlreader );
Executionpad. Child = DocumentRoot;
}
Catch (exception ex)
{
Textflow errortext = new textflow ();
Paragraph P = new paragraph ();
P. fontfamily = new fontfamily ("segoe UI ");
P. Foreground = brushes. Red;
P. Text = ex. message;
Errortext. horizontalalignment = horizontalalignment. Center;
Errortext. blocks. Add (P );
Executionpad. Child = errortext;
}
Finally
{
If (stringreader! = NULL) stringreader. Close ();

If (xmlreader! = NULL) xmlreader. Close ();
}

Note that when an exception is throw in this process, the exception message will be printed out in the executionpad which is the container for the uielement constructed from XAML input. the following is the snapshot shot of this little helpful application:

Xamlpad in execution View:


Xamlpad in XAML View:

For the complete source code, please check here.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.