【问题标题】:What is the hostComponent?什么是主机组件?
【发布时间】:2012-04-13 09:59:42
【问题描述】:

我在 Flex 中为一个进度条设置皮肤,在阅读了一些内容之后,我发现有一个叫做 hostComponent 的东西。

Adobe 网站说:

"The host component is the component that uses the skin. By specifying the host component, Spark skins can gain a reference to the component instance that uses the skin by using the hostComponent property."

但是,我仍然不明白这到底是如何工作的。

有什么快速实用的解释吗?

谢谢!

【问题讨论】:

标签: actionscript-3 apache-flex flex-spark skin


【解决方案1】:

当您在 Spark 架构中创建自定义组件时,通常会将它们分成两部分:

  • 包含自定义组件核心功能的 ActionScript 类。这个类通常会扩展 SkinnableComponent 或 SkinnableContainer
  • 一个 MXML 外观类,与该 ActionScript 类松散关联,并且仅包含组件的可视化表示。这个类不应该包含真正的功能,用另一个皮肤代替它应该是微不足道的。

从皮肤的角度来看,这两个类中的第一个被称为宿主组件。

一个简单的例子

让我们通过扩展 SkinnableContainer 创建一个非常简单的面板:

public class MyPanel extends SkinnableContainer {

    [Bindable]
    public var title:String;

}

如您所见,我创建了一个属性“title”,我们想用它来在面板中显示一个标题。现在让我们创建一个使用此属性的皮肤:

<?xml version="1.0" encoding="utf-8"?>
<s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark">

    <fx:Metadata>
        [HostComponent("path.to.MyPanel")]
    </fx:Metadata>

    <!-- graphics for title bar go here -->
    <s:Label text="{hostComponent.title}" top="5" left="5" />

    <!-- graphics for panel content go here -->
    <s:Group id="contentGroup" top="30" bottom="0" left="0" right="0" />

</s:Skin>

主机组件在“元数据”块中定义,您可以看到我们可以使用它将其属性绑定到我们的可视化表示中。 'contentGroup' 存在是因为 SkinnableContainer 需要它;这是您放入自定义面板中的所有元素。所以这里是如何使用它:

<myComps:MyPanel title="Panel title" skinClass="path.to.skins.MyPanelSkin">
    <s:Label text="Hello Panel" />
    <!--everything in here goes into the 'contentGroup'-->
</myComps:MyPanel>

【讨论】:

  • +1 来自我。我要补充一点,hostComponent 是皮肤类上的一个属性,它引用了组件类的一个实例。我相信该属性是基于 HostComponent 元数据“在幕后”创建的。它的存在在 ActionScript/Mobile 皮肤中更加明显;需要手动创建的文档。
猜你喜欢
  • 1970-01-01
  • 2016-09-10
  • 1970-01-01
  • 2017-08-26
  • 2012-09-05
  • 2018-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多