当前位置:编程学习 > C#/ASP.NET >>

资源文件与命名空间的关系很纠结

新建一个winform ,修改了基类,如以下的(伪)代码:
namespace N1{
    class Form : MyForm //MyForm 是从Forms::Form 派生的子类
}

通过在属性栏中为Form的Icon属性添加一个icon,编译通过,调试正常;

然后修改了Form的命名空间:
namespace N1{
    namespace N2{
        class Form : MyForm 
    }
}

编译通过,但是调试在加载时Form 的resources->GetObject 方法抛出MissingManifestResourceException异常!
虽然可以自已另外生成.resources文件解决… 但是,我只想放一个小小的iocn进去,却要搞得这么复杂不太值得,请问有没有简单方法可以解决这个问题? --------------------编程问答-------------------- 没有看懂,不懂 --------------------编程问答-------------------- 补充一下:MyForm 的命名空间是N1(N1.MyForm )
即使Form 的基类是System::Windows::Forms::Form,只要Form 处在嵌套的命名空间中就一定抛出异常,如果像N1.Form 或者N2.Form 这样非嵌套的命名空间就没有问题 --------------------编程问答-------------------- xamine your RESX files. Usually Visual Studio names a form's or a
component's resources it stores to the RESX files with the namespace name
included (because it uses quite a specific way of loading resources based by
the type name of the class to which the resource relates). Therefore, after
you have renamed the namespace, you should check the resource names and make
necessary edits manually.

P.S. Be sure to back up all files you are going to change!

参考:

MissingManifestResourceException after renaming namespace --------------------编程问答-------------------- 楼上的给的链接里没有详细一点的.resx修改例子,而且那边说的是简单修改命名空间名的问题,我是嵌套命名空间的问题…… 再说,IDE会在修改控件布局的时候更新.resx …… --------------------编程问答-------------------- to findcaiyzh 那个链接中的问题,一劳永逸的解决办法是修改.resx 属性中生成.resources文件的路径,简单兼快捷…… --------------------编程问答-------------------- 调用资源文件 命名空间要写对
给你个例子

private void buttonX1_Click(object sender, EventArgs e)
        {

            System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
            Stream streamOri, streanOut;
            BufferedStream bufferedOri, bufferedOut;
            byte[] buffer = new byte[1024];
            int byteread;

            streamOri = asm.GetManifestResourceStream("_60_1.Resources.clean.txt");//命名空间和文件要写对
            streanOut = File.OpenWrite(@"c:\clean.bat");
            bufferedOri = new BufferedStream(streamOri);
            bufferedOut = new BufferedStream(streanOut);
            byteread = streamOri.Read(buffer, 0, 1024);
            while (byteread > 0)
            {
                bufferedOut.Write(buffer, 0, byteread);
                byteread = bufferedOri.Read(buffer, 0, 1024);

            }
            bufferedOut.Flush();
            bufferedOri.Close();
            bufferedOut.Close();


            Process p = new Process();

            p.StartInfo.FileName = @"c:\clean.bat";


            //p.StartInfo.UseShellExecute = false;
            //p.StartInfo.RedirectStandardInput = true;
            //p.StartInfo.RedirectStandardOutput = true;
            //p.StartInfo.RedirectStandardError = true;
            //p.StartInfo.CreateNoWindow = true; 
          
            p.Start();

       
            p.WaitForExit();



            DialogResult res = MessageBox.Show(this, "是否查看详细记录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (res == DialogResult.Yes)
            {
                System.Diagnostics.Process.Start(SpecialDirectories.Temp + @"\一键清除系统垃圾.txt");
                File.Delete(@"c:\clean.bat");

            }
            else
            {
                File.Delete(@"c:\clean.bat");
                File.Delete(SpecialDirectories.Temp + @"\一键清除系统垃圾.txt");
            }
        }
--------------------编程问答-------------------- IDE 生成的代码是
this->Icon = (cli::safe_cast<System::Drawing::Icon^  >(resources->GetObject(L"$this.Icon")));

即使改成N1.N2.$this.Icon 问题依旧…… --------------------编程问答-------------------- 唉…… 还没有完美的答案么?自己不太安稳地解决了一下:
我用UE看了编译好的exe,证实了图标已经嵌入了,可就是读不出来,想了大半天终于发现问题所在:
VS的IDE不太智能,看楼顶的伪代码,我把Form 放到N2 命名空间下,向Form 加入图标图片什么的,VS认为是给N1.Form 添加的,它自已理所当然地按这个错误的命名空间把资源编译了…… 而托管资源的读取正是按照命名空间查找的,程序运行的时候,它跑去找N1.N2.Form 的资源了,结果当然是找不着…… 
这个时候,要把ComponentResourceManager 构造函数的参数改为N1.Form::typeid,问题初步解决……(这个N1.Form 以及它的typeid 不知道是从那里跑出来的… 囧一个 )
我说不太安稳是因为在IDE在修改它的布局时会把代码改回去…… 再囧一个…… --------------------编程问答--------------------
引用 8 楼 zlcnkkm 的回复:
唉…… 还没有完美的答案么?自己不太安稳地解决了一下:
我用UE看了编译好的exe,证实了图标已经嵌入了,可就是读不出来,想了大半天终于发现问题所在:
VS的IDE不太智能,看楼顶的伪代码,我把Form 放到N2 命名空间下,向Form 加入图标图片什么的,VS认为是给N1.Form 添加的,它自已理所当然地按这个错误的命名空间把资源编译了…… 而托管资源的读取正是按照命名空间查找的,程序运行……

囧了 --------------------编程问答-------------------- --------------------编程问答-------------------- 确实很纠结 加油
补充:.NET技术 ,  VC.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,