.net框架如何在类中嵌套表单。 (vb.net)

发布于 2024-08-04 16:26:33 字数 266 浏览 5 评论 0原文

如果我使用反射器查看框架中的某些类,我可以看到表单和用户控件被设为私有并嵌套到父类中。

例如,我有一个控件,它使用特定于该控件的弹出窗体。 目前,我使弹出表单朋友可以访问。 如果我想以框架方式完成它,我会将其设为私有并将其嵌套到控制类中。 但是,如果我这样做,我将无法再使用 ide 来设计表单,并且在尝试编译时会出现错误。 所以,我有两个问题:

(1)微软是否在最后一刻采取措施将所有内容嵌套为私有?

(2) 他们的方式是首选方式还是我应该坚持使用我的朋友访问器?

If I look at some classes in the framework, using reflector, I can see that forms and user controls are made private and nested into a parent class.

For instance, I have a control which makes use of pop-up form that is specific to that control.
At the moment, I make the pop-up form friend accessible.
If I wanted to do it the framework way, I'd make it private and nest it into the control class.
If I do this, however, I can no longer use the ide to design the form and I get errors when I try to compile.
So, I have 2 questions:

(1) Do Microsoft do something at the last minute to nest all things private?

(2) Is their way the preferred way or should I stick to my friend accessors?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵山崖 2024-08-11 16:26:33

嵌套形式更好,因为它强制执行正确的封装,并且意味着最终的控件将最终出现在一个漂亮整洁的包中以供分发。如果你不关心这些,就继续按照你的方式去做。但是,如果您至少想尝试嵌套该类,则可以执行以下操作:

  1. 使用设计器像平常一样在类外部构建嵌套表单。
  2. 添加第二个表单作为私有嵌套表单,就像在 CLR 示例中所做的那样,其名称与您在步骤 1 中构建的表单相同。
  3. *.designer.vb< 迁移代码第一个表单的 /code> 或 *.designer.cs 到第二个表单的构造函数。它主要只是一个大的复制/粘贴。
  4. 从步骤 1 中删除该表单。您可能希望通过将其移动到单独的类库项目来保留,以便在需要进行更改时可以使用。

The nested form is better, because it enforces correct encapsulation and means the final control will end up in one nice neat package for distribution. If neither of those are a concern for you keep doing it your way. But if you want to at least try nesting the class, you can do something like this:

  1. Use the designer to build your nested form outside the class as your normally would.
  2. Add a second empty form as a private nested form as they do in the CLR examples with the same name as the form you built in step 1.
  3. Migrate the code from *.designer.vb or *.designer.cs for your first form to the constructor for your 2nd form. It'll mostly be just a big copy/paste.
  4. Remove the form from step 1. You might want preserve by moving it to a separate class library project so you can use when you need to make changes.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文