StringBuilder xaml =
new StringBuilder();
xaml.Append(
"
<TextBlock
");
xaml.Append( " xmlns=\"http://schemas.microsoft.com/client/2007\" ");
xaml.Append( " FontWeight=\"Bold\" Text=\"动态创建XAML对象\" /> ");
// 创建TextBlock
TextBlock tb = (TextBlock)XamlReader.Load(xaml.ToString());
LayoutRoot.Children.Add(tb);
xaml = new StringBuilder();
xaml.Append( " <Button ");
xaml.Append( " xmlns=\"http://schemas.microsoft.com/client/2007\" ");
xaml.Append( " Content=\"a Button\" Name=\"btn1\" Tag=\"a tag\"/> ");
// 创建TextBlock
Button b = (Button)XamlReader.Load(xaml.ToString());
b.Click += buttonClick;
LayoutRoot.Children.Add(b);
本文来自RGY_LZY的博客,原文地址:http://www.cnblogs.com/computer-lzy/archive/2012/08/15/2640095.html