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

推荐订阅源

WordPress大学
WordPress大学
小众软件
小众软件
MongoDB | Blog
MongoDB | Blog
Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
L
LangChain Blog
大猫的无限游戏
大猫的无限游戏
量子位
A
About on SuperTechFans
G
Google Developers Blog
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园_首页
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
V
Visual Studio Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
U
Unit 42
Apple Machine Learning Research
Apple Machine Learning Research

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