MainPage.xaml
<UserControl x:Class="Hongcing.SilverlightApplication.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Loaded="LayoutRoot_Loaded" />
</UserControl>
MainPage.xaml.cs
1 using System.Windows;
2 using System.Windows.Controls;
3 using System.Windows.Markup;
4
5 namespace Hongcing.SilverlightApplication
6 {
7 public partial class MainPage : UserControl
8 {
9 public MainPage()
10 {
11 InitializeComponent();
12 }
13
14 private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
15 {
16 Panel panel = sender as Panel;
17
18 //注意:
19 //XAML 内容字符串必须定义单个根元素;必须是格式良好的 XML,并且必须是有效的 XAML。
20 //根元素必须指定某一默认的 XML 命名空间。
21 //通常是 Silverlight 命名空间 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"。
22 string xaml = "<TextBlock xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>Hello World!</TextBlock>";
23 UIElement element = XamlReader.Load(xaml) as UIElement;
24 panel.Children.Add(element);
25 }
26 }
27 }
28
在 Microsoft Visual Studio 2008 SP1 和 Silverlight 3 Tools 环境下编译通过。