一个把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. }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值