热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> Silverlight>>正文内容

SilverLight动态加载控件

发布时间:2010年05月18日点击数: 佚名

使用SilverLight动态加载控件XamlReader.Load的使用

1.生明一个StringBuilder用于连接字符串
2.创建命名空间(以及相应属性);
2.创建控件使用XamlReader.Load
3.加载到面板上面

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Net; 
  5. using System.Windows; 
  6. using System.Windows.Controls; 
  7. using System.Windows.Documents; 
  8. using System.Windows.Input; 
  9. using System.Windows.Media; 
  10. using System.Windows.Media.Animation; 
  11. using System.Windows.Shapes; 
  12. using System.Text; 
  13. using System.Windows.Markup; 
  14. namespace sl19 
  15.     public partial class MainPage : UserControl 
  16.     { 
  17.         public MainPage() 
  18.         { 
  19.             InitializeComponent(); 
  20.             ////textBlock里面的文本 
  21.             StringBuilder xaml = new StringBuilder(); 
  22.             xaml.Append("<TextBlock "); 
  23.             xaml.Append("xmlns=\"http://schemas.microsoft.com/client/2007\" "); 
  24.             xaml.Append("Canvas.Left=\"50\" Canvas.Top=\"30\"  FontSize=\"50\" "); 
  25.             xaml.Append(" FontWeight=\"Bold\" Text=\"动态创建XAML对象\" />"); 
  26.             //创建textBlock 
  27.             TextBlock textBlock = (TextBlock)XamlReader.Load(xaml.ToString()); 
  28.             parentCanvas.Children.Add(textBlock); 
  29.             //line的xaml文本 
  30.            xaml= new StringBuilder(); 
  31.             xaml.Append("<Line Stroke=\"Red\" "); 
  32.             xaml.Append("xmlns=\"http://schemas.microsoft.com/client/2007\" "); 
  33.             xaml.Append(" X1=\"30\" Y1=\"30\" "); 
  34.             xaml.Append(" X2=\"200\" Y2=\"200\"  StrokeThickness=\"3\" />"); 
  35.             //创建LINE对象 
  36.             Line line = (Line)XamlReader.Load(xaml.ToString()); 
  37.             parentCanvas.Children.Add(line);                        
  38.         } 
  39.     } 

总结:很简单的案例,但是自己按人家写出来发现也有点困难。

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作