ILSpy中baml转化为xaml的改进

 这些天,正在学习.Net的框架。

找到一个较大的工程,反编译来学习。

这个框架用到了当前所有能想到的东西,如prism,Infragistics公司的插件,如RIBBON,Layoutmgr,是基于WPF的。

其中,想了解一下LAYOUT切换的工过程,所以分析一下XAML。

所以用到了ILSpy。

从sourceforge下载了ILSpy的程序和源码。

但发现一个问题,当我们另存为时,得到的baml文件。很不方便。


但发现一个问题,当我们另存为时,得到的baml文件。很不方便


所以,打算自己改改ILSpy程序。

研究了一会,找到一个地方:

	public class ResourceEntryNode : ILSpyTreeNode
	{
		public override bool Save(DecompilerTextView textView)
		{
			SaveFileDialog dlg = new SaveFileDialog();
			dlg.FileName = Path.GetFileName(DecompilerTextView.CleanUpName(key));
			if (dlg.ShowDialog() == true) {
				data.Position = 0;
				using (var fs = dlg.OpenFile()) {
					data.CopyTo(fs);
				}
			}
			return true;
		}
	}

很快找到它的继承类:

添加了如下代码:


using System.Xml;
using System.Xml.Linq;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.ILSpy;
using ICSharpCode.ILSpy.TextView;
using ICSharpCode.ILSpy.TreeNodes;
using Mono.Cecil;
using Ricciolo.StylesExplorer.MarkupReflection;

using Microsoft.Win32;
using System.Text;


namespace ILSpy.BamlDecompiler
{
	public sealed class BamlResourceEntryNode : ResourceEntryNode
	{
        string curstr = "";


        public override bool Save(DecompilerTextView textView)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.FileName = Path.GetFileName(DecompilerTextView.CleanUpName(base.key));

            dlg.FileName=dlg.FileName.Replace(".baml", ".xaml");
            
            
            if (dlg.ShowDialog() == true)
            {
                string strCurFileName = dlg.FileName;

                   FileStream fs=null;

                    //定义文件信息对象
                    FileInfo finfo = new FileInfo(strCurFileName);

                     //判断文件是否存在以及是否大于
                    if (finfo.Exists)
                    {
                        //删除该文件
                        finfo.Delete();
                    }


                    //创建只写文件流
                    fs = finfo.OpenWrite();
                    //根据上面创建的文件流创建写数据流
                using (StreamWriter w=new StreamWriter(fs, Encoding.Default))
                {

                    //设置写数据流的起始位置为文件流的末尾
                    w.BaseStream.Seek(0, SeekOrigin.Begin);
                    w.Write(curstr);
                    w.Flush();
                    w.Close();
                }


            }
            return true;
        }
}

忘了这段代码:

		bool LoadBaml(AvalonEditTextOutput output)
		{
			var asm = this.Ancestors().OfType<AssemblyTreeNode>().FirstOrDefault().LoadedAssembly;
			Data.Position = 0;
			XDocument xamlDocument = LoadIntoDocument(asm.GetAssemblyResolver(), asm.AssemblyDefinition, Data);
            curstr = xamlDocument.ToString();
			output.Write(curstr);

			return true;
		}


搞定

代码:http://download.csdn.net/detail/haoyujie/5240897

可执行程序(懒人们有福了): http://download.csdn.net/detail/haoyujie/5240882

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值