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

推荐订阅源

G
Google Developers Blog
S
SegmentFault 最新的问题
Jina AI
Jina AI
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
B
Blog
博客园 - 【当耐特】
博客园 - Franky
M
MIT News - Artificial intelligence
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LangChain Blog
MyScale Blog
MyScale Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 叶小钗
Last Week in AI
Last Week in AI
Engineering at Meta
Engineering at Meta

博客园 - 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.