wpf学习笔记-如何获得MS定义好的模版

 1 private void Button_Click(object sender, RoutedEventArgs e)
 2         {
 3             string xaml = GetFrameworkTemplate(b2);
 4             FileStream fs = File.Open("" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xml", FileMode.OpenOrCreate, FileAccess.ReadWrite);
 5 
 6             byte[] bytes = Encoding.ASCII.GetBytes(xaml);
 7 
 8             fs.Write(bytes, 0, bytes.Length);
 9         }
10 
11         private String GetFrameworkTemplate(Control ctrl)
12         {
13             FrameworkTemplate tmp = ctrl.Template;
14 
15             string xaml = "";
16 
17             if (tmp != null)
18             {
19                 XmlWriterSettings setting = new XmlWriterSettings();
20                 setting.Indent = true;
21                 setting.IndentChars = new string(' ', 4);
22                 setting.NewLineOnAttributes = true;
23 
24                 StringBuilder builder = new StringBuilder();
25                 XmlWriter writer = XmlWriter.Create(builder, setting);
26 
27                 try
28                 {
29                     XamlWriter.Save(tmp, writer);
30                     xaml = builder.ToString();
31                 }
32                 catch (Exception ex)
33                 {
34 
35                     xaml = ex.Message;
36                 }
37             }
38             else {
39                 xaml = "no template.";
40             }
41 
42             return xaml;
43         }

 

posted @ 2014-03-25 18:42  Thirty  阅读(195)  评论(0编辑  收藏  举报