实例来自“天使坠”,由灵代写介绍之:
主要核心使用.Net Silverlight SDK的类库中委托语言如(C#)的动态创建控件方法,在初始化时调用了LoadXaml方法,并且使用System.Windows.Markup.XamlReader方法读取Xaml数据并创建对象,并使用this.LayoutRoot.Children.Add添加了创建的对象控件:
using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.IO; using System.Windows.Markup;
public partial class Page : UserControl { public Page() { InitializeComponent(); this.LoadXaml(); } private const string xaml = "<Rectangle xmlns=\"http://schemas.microsoft.com/client/2007\" Width=\"20\" Height=\"40\" Stroke=\"#808080\" StrokeThickness=\"1.5\" RadiusX=\"2\" RadiusY=\"2\" Opacity=\"1\">" + "<Rectangle.Fill>" + "<LinearGradientBrush StartPoint=\"0.5,0\" EndPoint=\"0.5,1\" >" + "<GradientStop Color=\"#0076E0\" Offset=\"0.0\" />" + "<GradientStop Color=\"#0046B0\" Offset=\"1.0\" />" + "</LinearGradientBrush>" + "</Rectangle.Fill>" + "</Rectangle>"; public void LoadXaml() { Rectangle rect = XamlReader.Load(xaml) as Rectangle; this.LayoutRoot.Children.Add(rect); }
}// end for method
|