











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)
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();
}
XamlPad in Execution View:
XamlPad in Xaml View:
For the complete source code,please check here.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。