在Compact Framework中获取组件的父容器 [英] Get Component's Parent Container in Compact Framework

查看:46
本文介绍了在Compact Framework中获取组件的父容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上需要执行与以下操作相同的操作:获取组件的父表单

I basically need to do the same thing as in: Get Component's Parent Form

但是在紧凑型框架中,没有定义

IDesignerHost

有什么想法吗?

我想将它放在 System.Windows.Forms.Container 控件上,这是 System.ComponentModel.Component 派生的组件.我需要参考这一点的原因是,我不想以将要放置组件的每种形式为组件手动设置父容器".

It is System.ComponentModel.Component derived component that I want to drop on some System.Windows.Forms.Container control. The reason I need to reference to this is that I don't want to manually set Parent Container for my component in each form I will drop component to.

推荐答案

在Compact Framework中,窗体的组件包含在成员Container变量内.除了Form创建实例的事实以外,该Container与Form本身之间没有直接关系.为此,该实例可以轻松地传递给其他Form或非Form类,并可以传递给原始父Dispose.

In the Compact Framework a Form's components are contained inside a member Container variable. There is no direct relation between that Container and the Form itself, other than the fact the Form created the instance. That instance could easily be passed to some other Form or non-Form class, for that matter, and the original parent Disposed.

这意味着无法确定组件的包含表格".这些信息根本不会存储在任何地方(GC可以找到根,但这有点不同,而且无论如何都无法查询).

What this means is that there is no way to determine the "containing Form" of a Component. That information simply isn't stored anywhere (well the GC sees the root, but that's a bit different and not queryable anyway).

现在,如果您只想引用IContainer,而不是将实例保存到IContainer的Form,则可以查询 component.Site.Container ,但即使这样,也有大量潜在的漏洞在里面.

Now if you only want a reference to the IContainer, and not the Form that holds the instance to the IContainer, you can query component.Site.Container, but even that's got loads of potential holes in it.

例如,如果将SerialPort组件拖放到窗体上,则设计者会将其添加到组件集合中,并设置 serialPort.Site.Container .但是,如果将Timer组件拖放到窗体上,则将添加到Components集合中,因此 timer.Site 实际上是 null ,因此即使尝试查询Container也会抛出NullReferenceException.

For example, if you drop a SerialPort Component onto a Form, it gets added to the components collection by the designer and serialPort.Site.Container gets set. However, if you drop a Timer Component onto a Form, the Timer is not added to the Components collections, so timer.Site is actually null, so even trying to query the Container would throw a NullReferenceException.

为了使它更有趣,像SerialPort这样的组件也具有允许在没有IContainer的情况下进行创建的构造函数,因此,如果开发人员手动创建SerialPort,则将SerialPort放到设计器中的Form上会将其添加到Form的Components中,很少将其完全添加到Components集合中.

To make it more fun, Components like the SerialPort also have constructors that allow creation without an IContainer, so while dropping the SerialPort onto a Form in the designer will add it to a Form's Components, if a developer creates the SerialPort manually, it's pretty rare that it will get added to the Components collection at all.

基本上,我认为您需要重新考虑您想要的体系结构.您必须提出一些不依赖于父容器的机制.

Basically, I think you need to rethink your intended architecture. You'll have to come up with some mechanism that doesn't depend on the parent Container.

这篇关于在Compact Framework中获取组件的父容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆