Flex:SDK 版本 4.1 和 4.5 之间的 parentDocument 不一致 [英] Flex: parentDocument inconsistent between SDK version 4.1 and 4.5

查看:26
本文介绍了Flex:SDK 版本 4.1 和 4.5 之间的 parentDocument 不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 Flex SDK 4.1 和 4.5 之间的 parentDocument 存在一些不一致.简而言之,在 4.1 中工作的现有代码在 4.5(和 4.6)中被破坏.下面是一个小例子来演示:

I'm seeing some inconsistencies with parentDocument between Flex SDK 4.1 and 4.5. In a nutshell, existing code that works in 4.1 is broken in 4.5 (and 4.6). Here's a small example to demonstrate:

TestProject.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx"
               minWidth="500" minHeight="500" xmlns:local="*">

    <local:SubComponent x="50" y="50"/>
</s:Application>

SubComponent.mxml

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

    <fx:Script>
        <![CDATA[
            import spark.components.Application;

            protected function button1_clickHandler(event:MouseEvent):void
            {
                var app:Application = this.parentDocument as Application;

                if (app != null) {
                    trace('Good');
                } else {
                    trace('Bug');
                }
            }
        ]]>
    </fx:Script>

    <s:Button x="18" y="20" label="Button" click="button1_clickHandler(event)"/>
</s:Group>

使用 4.1 SDK,我可以通过 parentDocument 成功检索 Application 对象.但是,这在 4.5 SDK 中失败了.在 4.5 中,parentDocument 现在是一个皮肤对象:

With the 4.1 SDK, I'm successfully able to retrieve the Application object via parentDocument. However, this fails with the 4.5 SDK. With 4.5, parentDocument is now a skin object:

"parentDocument"    spark.skins.spark.ApplicationSkin (@9d7e479)

如果我上两层,那么我就成功拿到了Application对象:

If I go up two levels, then I successfully get the Application object:

"parentDocument.parentDocument"    TestProject (@9d7e0a1)   

因此,我可以通过将 parentDocument 替换为 parentDocument.parentDocument 来解决这个问题.但是,我不清楚这种变化是否是我可以指望保持一致的预期行为,或者它是否是一个可能在未来某个时候修复的错误,届时行为将恢复到 4.1 方式做着东西.所以,我的问题是:

So, I could work around the problem by replacing parentDocument with parentDocument.parentDocument. However, I'm not clear whether this change is expected behavior that I can count on being consistent going forward, or if it's a bug that will likely be fixed sometime in the future, at which point the behavior will revert to the 4.1 way of doing things. So, my questions are:

  1. 有其他人遇到过这个问题吗?如果是,你是如何处理的
  2. 如果您已经制定了解决方法,那么您的解决方案是否取决于特定的 SDK 版本,还是适用于所有 SDK 版本?
  3. 从 4.1 到 4.5 的行为变化是有意的,还是这是一个错误?我怀疑是 bug,但我一直找不到任何东西以一种或另一种方式确定.

请注意,我上面的示例旨在以最少的代码和最小的复杂性来演示问题.在我的实际项目中,我有一个包含带有子组件的 TitleWindow 的模块,我试图让子组件调用 TitleWindow 中的方法(通过 parentDocument).因此,涉及查找 Application 对象的其他方法的解决方法实际上并不适用,因为子组件实际上是在寻找 TitleWindow,而不是 Application>.

Note that my example above is contrived to demonstrate the problem with the least amount of code and minimal complexity. In my actual project, I have a module that contains a TitleWindow with subcomponents, and I'm trying to have the subcomponents call methods in the TitleWindow (via parentDocument). So, workarounds that involve other ways of finding the Application object aren't really applicable because the subcomponents are actually looking for a TitleWindow, not the Application.

推荐答案

是的,这不是错误.这实际上是火花组件的一个功能".基本上,对于 SkinnableContainers(即 TitleWindow),子项实际上被添加到组件皮肤的 contentGroup 中.有几篇很好的文章解释了这一切 at这个链接也是这个.在许多情况下,您可能可以快速将代码更改为 .owner 属性(只要子对象是 DisplayObject),以实现您想要的结果.更好的方法可能是重构您的一些代码以更好地适应外观提供的视图封装(让子组件触发事件而不是尝试访问父组件上的方法......那种事情).不过,一旦您了解了皮肤的所有内部工作原理,这将是非常酷的东西.

Yeah, this isn't a bug. This is actually a "feature" of the spark components. Basically, for SkinnableContainers (which TitleWindow is) the children are actually added to the contentGroup of the components skin. There are a few good articles explaining it all at this link and also this one. You can probably quickly change your code to the .owner property (as long as the child object is a DisplayObject) in many scenarios to achieve the result you want. A better approach might be to refactor some of your code to better fit with the view encapsulation that skins provide (have the child components fire events rather than trying to access methods on the parent... that kinda thing). it's pretty cool stuff once you understand all of the inner workings of skins though.

希望这会有所帮助.

这篇关于Flex:SDK 版本 4.1 和 4.5 之间的 parentDocument 不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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