CompositeControl 和 XML 反序列化设计时错误
我收到“错误渲染控件”错误,该错误仅当我在 desgin 模式下将控件放置在 Web 表单上时才会发生,如果我运行页面,控件就会正确显示。
上面的语句并不重要,发生这个错误是因为返回的toolbars对象为null。
调试后,问题出在从 CeateChildControls() 调用的函数中:
public static ToolBars LoadToolbarsFromConfigFile()
{
ToolBars toolbars;
Assembly executingAssembly = Assembly.GetExecutingAssembly();
string resource = "Editor.ConfigFiles.ToolBars.xml";
using (Stream stream = executingAssembly.GetManifestResourceStream(resource))
{
XmlSerializer serializer = new XmlSerializer(typeof(ToolBars));
toolbars = (serializer.Deserialize(stream)) as ToolBars;
}
return toolbars;
}
工具栏返回 null! (在设计模式下)
但是当我运行该页面时,工具栏会返回适当的数据。
如果您需要有关我的代码的更多信息,请询问。
更新:
它一定是与Assembly有关的东西, 如果我使用文件流代替指定文件,它确实有效。
另一个更新:
我稍微修改了我的代码,并添加了“数据集”用于测试目的:
using (DataSet ds = new DataSet())
{
ds.ReadXml(typeof(TheEditor).Assembly.GetManifestResourceStream("Editor.ConfigFiles.ToolBars.xml"));
//show message box to see if it works
System.Windows.Forms.MessageBox.Show(ds.Tables.Count.ToString());
}
我注意到的另一件事是,当我将控件添加到新的网站项目时,上述所有情况都会发生,但是如果我设置控件项目的调试属性来启动外部程序(我启动 Visual Studio),然后创建一个新项目并添加一切正常的控件。
I get "Error rendering control" error that only happens when I place the control on the webform in desgin-mode, if I run the page the control is displayed correctly.
The above statement is not important, this error happens because the returned toolbars object is null.
After debugging, the problem is in a function that is called from CeateChildControls():
public static ToolBars LoadToolbarsFromConfigFile()
{
ToolBars toolbars;
Assembly executingAssembly = Assembly.GetExecutingAssembly();
string resource = "Editor.ConfigFiles.ToolBars.xml";
using (Stream stream = executingAssembly.GetManifestResourceStream(resource))
{
XmlSerializer serializer = new XmlSerializer(typeof(ToolBars));
toolbars = (serializer.Deserialize(stream)) as ToolBars;
}
return toolbars;
}
the toolbars returns null! (in design-mode)
But when I run the page, toolbars returns appropriate data.
If you need some more info about my code please ask.
Update:
It must be something with Assembly,
If I use file stream instead with specified file, it does work.
Another UPDATE:
I've modified my code a bit, and added "dataset" for test purpose:
using (DataSet ds = new DataSet())
{
ds.ReadXml(typeof(TheEditor).Assembly.GetManifestResourceStream("Editor.ConfigFiles.ToolBars.xml"));
//show message box to see if it works
System.Windows.Forms.MessageBox.Show(ds.Tables.Count.ToString());
}
Another thing that I noticed, all above happens when I add my control to a new website project, but if I set debug property of control's project to start external program (i start visual studio) , and there I create a new project and add the control everything works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在 Visual Studio 中运行它时,您没有应用程序上下文,因此您无法对其进行“GetExecutingAssembly” - 或者更准确地说,执行程序集是 devenv.exe,并且它没有资源你正在寻找。
您可以使用
设计模式
属性来检查并查看您是否在 Visual Studio 中呈现该控件,并适当地修改您的行为:
或者,您可以在调用代码中执行检查,但由于这是一个公共方法,不能保证所有调用者都会执行此检查,因此您最好在该方法中执行此检查。
最后一个选择是 创建一个重写 LoadToolbarsFromConfigFile 方法并为您提供虚拟工具栏的设计器类。
When you are running this within Visual Studio, you don't have an Application context, and so you can't "GetExecutingAssembly" on it - or more accurately, the Executing Assembly is devenv.exe, and that doesn't have the resources you're looking for.
You can use the
DesignMode
property of the control to check and see if you are rendering the control within Visual Studio, and modify your behaviour appropriately:Alternatively, you could perform the check in the calling code, but as this is a public method, there's no guarantee that all callers would perform this check, so you're better off doing it in the method.
A final option would be to create a Designer class that overrides the LoadToolbarsFromConfigFile method and supplies a dummy toolbar for you.