且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何更新基于属性值设计时用户控件界面?

更新时间:2023-10-02 21:25:04

我不得不想回位到UserControlDesigner code ...

I'd have to think back a bit to the UserControlDesigner code...

简单地说:我不认为这是可能的。

Short story: I don't think it's possible.

和这里的很长的故事:

从我记得,位于ASCX文件的用户控件从不在设计运行。也就是说,ASCX或ASCX.CS文件内的code为从未编译或在Visual Studio中运行。这是造成的事实,即在CLR不能卸载已加载的程序集prevent内存泄漏。为了运行在你的用户控件code,Visual Studio中必须编译ASCX成DLL,然后加载它,然后运行code。您更改到ASCX每一次,它会再次执行此操作。每当这个操作发生更多的内存将从您的ASCX产生的额外加载的DLL被消耗掉。

From what I recall, User Controls located in ASCX files are never run in the designer. That is, the code inside the ASCX or ASCX.CS file is never compiled or run in Visual Studio. This is to prevent memory leaks caused by the fact that in the CLR you cannot unload assemblies that you have loaded. In order to run the code in your User Control, Visual Studio would have to compile your ASCX into a DLL, then load it, and then run the code. Every time you make a change to the ASCX, it would have to perform this operation again. Every time this operation happens more memory will be consumed by the additionally loaded DLL generated from your ASCX.

。相反,它解析ASCX文件,并查找其内部控制和加载这些控件来代替。对于找到的ASCX文件中的每个控制它会创建相关的控制设计者和呈现控件的设计时HTML。

Because of this limitation in the CLR the User Control designer doesn't actually compile or run the ASCX file. Instead, it parses the ASCX file and looks for controls inside it and it loads those controls instead. For each control it finds in the ASCX file it will create the associated control designer and render that control's design time HTML.

有几个方法可以解决此问题:

There are a couple of ways to work around this:


  1. 而不是使用ASCX用户控件,您可以编写从控制和code派生被写在CS或VB文件定期自定义控件。

  2. 编译ASCX成DLL。大卫Ebbo写一篇关于如何做到这一点的博客文章

  1. Instead of using an ASCX user control you can write a regular custom control that derives from Control and the code is written in a CS or VB file.
  2. Compile the ASCX into a DLL. David Ebbo wrote a blog post on how to do this.

这是这两种解决方案应该工作的原因是,它们都涉及到具有code编译成一个DLL。这个想法是,该DLL不经常改变,因此它是安全的Visual Studio加载DLL而无需重新加载它每次修改DLL(和内存泄漏)的风险。

The reason that these two solutions should work is that they both involve having the code compiled into a DLL. The idea is that the DLL doesn't change very often so it is safe for Visual Studio to load the DLL without risk of having to reload it for each time the DLL changes (and leak memory).