简体   繁体   English

Flex:如何创建一个全新的组件?

[英]Flex: How to create an entirely new component?

I'd like to develop a network graph application for Flex - imagine placing nodes on a Canvas and connecting them with links. 我想为Flex开发一个网络图应用程序 - 想象一下将节点放在Canvas上并用链接连接它们。 The nodes should have editable text and other UI components. 节点应具有可编辑的文本和其他UI组件。

I'm trying to find examples of creating an entirely new UI component from scratch, but all I've been able to find are trivial examples that extend existing components: a RedButton that extends Button, for example, or a ComboBox that has states to choose from. 我试图找到从头开始创建一个全新的UI组件的示例,但我能够找到的只是扩展现有组件的简单示例:例如,扩展Button的RedButton,或者具有状态的ComboBox从中选择。

My main question is, what ActionScript method defines the drawing of a component? 我的主要问题是, ActionScript方法定义了组件的绘制? What is the ActionScript equivalent of Java's paint() method? 什么是Java的paint()方法的ActionScript等价物?

You want to create a component that overrides the updateDisplayList method, and do your drawing in there: 您想要创建一个覆盖updateDisplayList方法的组件,并在那里进行绘制:

override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
  super.updateDisplayList( unscaledWidth, unscaledHeight );

  // The drawing API is found on the components "graphics" property
  graphics.clear();
  graphics.lineTo( 0, unscaledWidth );
  // etc
}

More information can be found here: http://livedocs.adobe.com/flex/3/html/help.html?content=ascomponents_advanced_3.html 可以在此处找到更多信息: http//livedocs.adobe.com/flex/3/html/help.html?content = ascomponents_advanced_3.html

I would suggest looking at the flexlib project if you need examples of custom components. 如果您需要自定义组件的示例,我建议您查看flexlib项目。

There's good general info in the livedocs here 有一个在LiveDocs中很好的通用信息在这里

Although you can create custom components in MXML and in ActionScript, I would recommend implementing them in ActionScript. 虽然您可以在MXML和ActionScript中创建自定义组件,但我建议您在ActionScript中实现它们。

In short this is what you need to do: 简而言之,这就是你需要做的:

When you create a custom component in ActionScript, you have to override the methods of the UIComponent class. 在ActionScript中创建自定义组件时,必须覆盖UIComponent类的方法。 You implement the basic component structure, the constructor, and the createChildren(), commitProperties(), measure(), layoutChrome(), and updateDisplayList() methods. 您实现了基本组件结构,构造函数以及createChildren(),commitProperties(),measure(),layoutChrome()和updateDisplayList()方法。

I recommend reading the 5-part tutorial series by Peter Ent on creating custom components. 我建议阅读Peter Ent关于创建自定义组件的5部分教程系列。 Here is the link to Part 1. 是第1部分的链接。

Also recommended, tutorial series on ItemRenderers and ItemEditors . 还推荐了关于ItemRenderersItemEditors的教程系列。

Chapter 19 of Programming Flex 2 by Kazoun and Lott show the construction event model as well as the refresh event model. Kazoun和Lott编写的Flex 2的第19章显示了构造事件模型以及刷新事件模型。

In short, the Flex component structure assumes that you have properties of the object that affect its appearance. 简而言之,Flex组件结构假定您具有影响其外观的对象属性。 Instead of applying changes immediately, it allows property mutators to invalidate the component's "content" (eg text) via the inherited invalidateProperties() method. 它不是立即应用更改,而是允许属性更改器通过继承的invalidateProperties()方法使组件的“内容”(例如文本)无效。 One also calls invalidateSize() when a property change will, ummm, change the size of the component, and invalidateDisplayList() when things need to be re-drawn (as opposed to just having different text). 当一个属性改变,ummm,改变组件的大小,以及当需要重新绘制事物时(而不是只有不同的文本),invalidateDisplayList(),也会调用invalidateSize()。

This invalidation model optimizes the work of the component. 此失效模型可优化组件的工作。 It does not measure itself unless it knows it changed size; 它不会衡量自己,除非它知道它改变了大小; it does not place and draw itself unless something called invalidateDisplayList() since the last time it updated its layout; 它不会放置并绘制自己,除非上次更新其布局时调用了invalidateDisplayList(); and it does not move its properties' values into its subcomponents unless it knows those values have changed. 并且它不会将其属性的值移动到其子组件中,除非它知道这些值已更改。

Cheers 干杯

The typical behavior is to subclass an existing component, mainly because so much of the work of implementing the functionality of so many of the most typical kinds of components is already done for you -- you just have to know which component most resembles the one you want to create. 典型的行为是对现有组件进行子类化,主要是因为实现了许多最典型的组件功能的大量工作已经为您完成 - 您只需要知道哪个组件最类似于您的组件想要创造。 (A challenge in its own right, given how many there are.) (考虑到有多少,本身就是一项挑战。)

If you like, though, you can create a pretty bare-bones UIComponent "from scratch" simply by extending UIComponent (which'll give you all the baseline stuff), or I suppose, if you're really going for minimalism, and you're up for a challenge, you can just implement IUIComponent , and define each interface method manually. 但是,如果您愿意,只需通过扩展UIComponent (它将为您提供所有基线内容),您可以“从头开始”创建一个非常简单的UIComponent ,或者我想,如果您真的想要极简主义,那么为了迎接挑战,你可以实现IUIComponent ,并手动定义每个接口方法。 If you do choose to go that second route, do yourself a favor and listen to this talk first -- it's an in-depth discussion of the Flex component architecture, given by one of the engineers on the Flex team. 如果您确实选择了第二条路线,那么请帮自己一个忙,先听听这个讲座 - 这是对Flex组件架构的深入讨论,由Flex团队的一位工程师给出。 I recommend it often, and highly; 我经常和高度推荐它; it's excellent, and it clarifies a number of component-development details that still aren't all that well documented. 它非常出色,它澄清了许多组件开发细节,但仍未完全记录。 Extremely valuable stuff if you're going to be building non-trivial custom components of the kind you're describing. 如果您要构建您所描述类型的非平凡自定义组件,那将是非常有价值的东西。

Hope it helps. 希望能帮助到你。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM