惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
小众软件
小众软件
美团技术团队
Martin Fowler
Martin Fowler
爱范儿
爱范儿
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
宝玉的分享
宝玉的分享
J
Java Code Geeks
B
Blog
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
B
Blog RSS Feed
博客园 - Franky

博客园 - Sheva

Two Xaml Tricks - Sheva I See Cool Stuff Today DataTemplate Is A Bit Naughtier To Play With XmlnsDefinitionAttribute Is Pretty Nifty Application.DoEvents In WPF 征召译者 C# Trivia Test CreateParams Is Pretty Cool! In-Place Editing In WPF SingleLinkedList Implementation Data Binding In WPF C# Application Markup Language Regex 101 Exercise I10 - Extract repeating hex blocks from a string AvalonPaint: Features Added Regex 101 Exercise I9 - Count the number of matches Crossbow: Hosting WPF Controls In Winforms Application Crossbow? WTF? The Architecture Of Avalon Drop Shadow Effect In WPF
XamlReader.Load(): Build Up Your Own XamlPad
Sheva · 2006-02-21 · via 博客园 - Sheva

    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 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 sreenshot of this little helpful application:

XamlPad in Execution View:


XamlPad in Xaml View:

For the complete source code,please check here.