Xaml转换成C#的方法

@[TOC]Xaml转换成C#的方法

Xaml转换成C#的方法

你好! 这是从其他博文代码改写编译通过而成的关于Xaml转换成C#的方法。代码经过调试,可以运行。部分代码予以省略。

新的代码

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Text;
using System.IO;

namespace xaml2cs
{
	/// <summary>
	/// Description of xaml2cs.
	/// </summary>
	public partial class xaml2csControl : UserControl
	{
		public xaml2csControl()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		void SourceTextBoxTextChanged(object sender, EventArgs e)
		{
			StringBuilder myCache = new StringBuilder(this.sourceTextBox.Text.Length);            
			myCache.Append("{\r\n");            
			myCache.Append(" string xaml = \r\n");            
			StringReader reader = new StringReader(this.sourceTextBox.Text);            
			string controlName = string.Empty;            
			string firstLine = string.Empty;            
			string  line =  string.Empty;
			
			int count = 0;            
			while ((line = reader.ReadLine()) != null)            
			{                //第一行                 
				if (count == 0)                
				{                    
					line = line.Trim();                    
					if (line.StartsWith("<") == true)                    
					{                        
						int space = line.IndexOf(' ');                        
						//controlName = line.Substring(1, space);
						controlName = line.Substring(1, space);  						
						firstLine += line.Substring(0, space);                        
						firstLine += " xmlns=\"http://schemas.microsoft.com/client/2007/\" " ; 
						firstLine += line.Substring(space, line.Length - space);                    
					}                    
					line = firstLine;                
				}                
				count++;                
				//第一行
				myCache.Append("\"");
				foreach (char c in line.ToCharArray())
				{
					if (c == '"')	myCache.Append('/');
				    myCache.Append(c);
				    
				}
				myCache.Append("\"\r\n");            
			            
			myCache.Remove( myCache.Length - 4, 4);
			myCache.Append( ";\r\n");
			myCache.Append( string.Format("{0} {1} = ({0})System.Windows.Markup.XamlReader.Load(xaml);\r\n", controlName, controlName.ToLower()));
			myCache.Append( "}\r\n");
			this.resultTextBox.Text=myCache.ToString();
		}
	}
	}
	}

namespace xaml2cs
{
	partial class xaml2csControl
	{
		/// <summary>
		/// Designer variable used to keep track of non-visual components.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
		private System.Windows.Forms.TextBox sourceTextBox;
		private System.Windows.Forms.TextBox resultTextBox;
		private System.Windows.Forms.Label Source;
		private System.Windows.Forms.Label result;
		/// <summary>
		/// Disposes resources used by the control.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}
		
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent()
		{
			this.sourceTextBox = new System.Windows.Forms.TextBox();
			this.resultTextBox = new System.Windows.Forms.TextBox();
			this.Source = new System.Windows.Forms.Label();
			this.result = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// sourceTextBox
			// 
			this.sourceTextBox.AcceptsReturn = true;
			this.sourceTextBox.Location = new System.Drawing.Point(29, 32);
			this.sourceTextBox.Multiline = true;
			this.sourceTextBox.Name = "sourceTextBox";
			this.sourceTextBox.Size = new System.Drawing.Size(590, 227);
			this.sourceTextBox.TabIndex = 0;
			this.sourceTextBox.TextChanged += new System.EventHandler(this.SourceTextBoxTextChanged);
			// 
			// resultTextBox
			// 
			this.resultTextBox.AcceptsReturn = true;
			this.resultTextBox.Location = new System.Drawing.Point(29, 287);
			this.resultTextBox.Multiline = true;
			this.resultTextBox.Name = "resultTextBox";
			this.resultTextBox.Size = new System.Drawing.Size(590, 245);
			this.resultTextBox.TabIndex = 1;
			// 
			// Source
			// 
			this.Source.Location = new System.Drawing.Point(29, 4);
			this.Source.Name = "Source";
			this.Source.Size = new System.Drawing.Size(100, 23);
			this.Source.TabIndex = 2;
			this.Source.Text = "Source";
			// 
			// result
			// 
			this.result.Location = new System.Drawing.Point(29, 261);
			this.result.Name = "result";
			this.result.Size = new System.Drawing.Size(100, 23);
			this.result.TabIndex = 3;
			this.result.Text = "Result";
			// 
			// xaml2cs
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.result);
			this.Controls.Add(this.Source);
			this.Controls.Add(this.resultTextBox);
			this.Controls.Add(this.sourceTextBox);
			this.Name = "xaml2csControl";
			this.Size = new System.Drawing.Size(656, 558);
			this.ResumeLayout(false);
			this.PerformLayout();

		}
	}
	}

功能样板演示代码:

<Window x:Class="ICSharpCode.XamlBinding.Tests.CompletionTestsBase"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
	<Grid>
	</Grid>
</Window>

结果代码:

{
 string xaml = 
"<Window xmlns=/"http://schemas.microsoft.com/client/2007//"  x:Class=/"ICSharpCode.XamlBinding.Tests.CompletionTestsBase/;
Window  window  = (Window )System.Windows.Markup.XamlReader.Load(xaml);
}
"	xmlns=/"http://schemas.microsoft.com/winfx/2006/xaml/presentation/;
Window  window  = (Window )System.Windows.Markup.XamlReader.Load(xaml);
}
"	xmlns:x=/"http://schemas.microsoft.com/winfx/2006/xaml/";
Window  window  = (Window )System.Windows.Markup.XamlReader.Load(xaml);
}
"	<Grid;
Window  window  = (Window )System.Windows.Markup.XamlReader.Load(xaml);
}
"	;
Window  window  = (Window )System.Windows.Markup.XamlReader.Load(xaml);
}
"	</Grid;
Window  window  = (Window )System.Windows.Markup.XamlReader.Load(xaml);
}
"</Window;
Window  window  = (Window )System.Windows.Markup.XamlReader.Load(xaml);
}

功能说明

XAML代码类似于:

XAML:<Window></Window>
XAML:<Page></Page>
XAML:<Panel></Panel>

对应C#代码可能为:

C#:Window w=new Window();
C#:Page p=new Page();
C#:Panel panel = new Panel();

最后,运行的图片如下图

上一节的代码示例图
此处,图片省略了画出Form表单的实际样子,对此表示抱歉。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值