一个把xaml转换C#动态生成代码的小工具(源代码)

测试页面:  http://silverlight.services.live.com/invoke/84388/xaml2obj/iframe.html

  1. using System.IO;
  2. using System.Text;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. namespace xaml2obj
  6. {
  7.     public partial class Page : UserControl
  8.     {
  9.         private TextBox sourceTextBox = new TextBox();
  10.         private TextBox resultTextBox = new TextBox();
  11.         public Page()
  12.         {
  13.             InitializeComponent();
  14.             this.sourceTextBox.AcceptsReturn = true;
  15.             this.resultTextBox.AcceptsReturn = true;
  16.             this.sourceTextBox.TextWrapping = TextWrapping.Wrap;
  17.             this.resultTextBox.TextWrapping = TextWrapping.Wrap;
  18.             Canvas canvas = new Canvas();
  19.             canvas.Width = 800;
  20.             canvas.Height = 600;
  21.             this.sourceTextBox.Width = 600;
  22.             this.sourceTextBox.Height = 300;
  23.             this.resultTextBox.Width = 600;
  24.             this.resultTextBox.Height = 300;
  25.             this.resultTextBox.SetValue(Canvas.TopProperty, 300.0);
  26.             canvas.Children.Add(this.sourceTextBox);
  27.             canvas.Children.Add(this.resultTextBox);
  28.             this.LayoutRoot.Children.Add(canvas);
  29.             this.sourceTextBox.TextChanged += new TextChangedEventHandler(sourceTextBox_TextChanged);
  30.         }
  31.         void sourceTextBox_TextChanged(object sender, TextChangedEventArgs e)
  32.         {
  33.             StringBuilder buffer = new StringBuilder(this.sourceTextBox.Text.Length);
  34.             buffer.Append("{/n");
  35.             buffer.Append(" string xaml = /n");
  36.             StringReader reader = new StringReader(this.sourceTextBox.Text);
  37.             string controlName = string.Empty;
  38.             string firstLine = string.Empty;
  39.             string line;
  40.             int count = 0;
  41.             while ((line = reader.ReadLine()) != null)
  42.             {
  43.                 //第一行
  44.                 if (count == 0)
  45.                 {
  46.                     line = line.Trim();
  47.                     if (line.StartsWith("<") == true)
  48.                     {
  49.                         int space = line.IndexOf(' ');
  50.                         controlName = line.Substring(1, space);
  51.                         firstLine += line.Substring(0, space);
  52.                         firstLine += " xmlns=/"http://schemas.microsoft.com/client/2007/" ";
  53.                         firstLine += line.Substring(space, line.Length - space);
  54.                     }
  55.                     line = firstLine;
  56.                 }
  57.                 count++;
  58.                 //第一行
  59.                 buffer.Append("/"");
  60.                 foreach (char c in line)
  61.                 {
  62.                     if (c == '/"') buffer.Append('//');
  63.                     buffer.Append(c);
  64.                 }
  65.                 buffer.Append("/" + /n");
  66.             }
  67.             buffer.Remove(buffer.Length - 4, 4);
  68.             buffer.Append(";/n");
  69.             buffer.Append(string.Format("{0} {1} = ({0})System.Windows.Markup.XamlReader.Load(xaml);/n", controlName, controlName.ToLower()));
  70.             buffer.Append("}/n");
  71.             this.resultTextBox.Text = buffer.ToString();
  72.         }
  73.     }
  74. }

猜你喜欢

转载自blog.csdn.net/KAMILLE/article/details/3432361