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

问题描述

在Flex SDK 4.1和4.5之间,我发现与 parentDocument 有一些不一致的地方。简而言之,4.1中的现有代码在4.5(和4.6)中被打破。下面是一个小例子:
$ b

TestProject.mxml

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

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

SubComponent.mxml

 <?xml version =1.0encoding =utf-8?> 
xmlns:s =library://ns.adobe.com/flex/spark
xmlns:mx =library://ns.adobe.com/flex/mx
width =200height =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:Group>

使用4.1 SDK,我可以成功检索 Application 对象通过 parentDocument 。但是,使用4.5 SDK会失败。在4.5中, parentDocument 现在是一个外观对象:

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

如果我上升两级,获得应用程序对象:

pre $ $ $ $ $ $
$

所以,我可以通过用 parentDocument替换 parentDocument .parentDocument 。然而,我不清楚这种改变是否是预期的行为,我可以指望未来一致,或者如果这是一个将来可能会被修复的错误,那么行为将会恢复到4.1的方式。做事。所以,我的问题是:


  1. 是否有其他人遇到这个问题?如果是这样的话,你是如何处理
    的呢?

  2. 如果你已经开发了一个解决方法,你的解决方案是依赖于
    a特定的SDK版本,还是它适用于所有SDK版本?

  3. 故意将行为从4.1更改为4.5,还是
    这是一个错误?我怀疑是bug,但我一直无法找到任何
    明确的方式。
  4. 上面的例子是用最少的代码和最小的复杂度来展示问题的。在我的实际项目中,我有一个包含子元件的 TitleWindow 的模块,我试图让子组件调用 TitleWindow (通过 parentDocument )。因此,涉及其他找到 Application 对象的方法是不适用的,因为子组件实际上正在寻找 TitleWindow 应用程序

    解决方案

    一个错误。这实际上是火花组件的特征。基本上,对于SkinnableContainers(即TitleWindow),子项实际上被添加到组件皮肤的contentGroup中。有一些很好的文章解释了这一切和。您可能很快就可以将代码快速更改为.owner属性(只要子对象是DisplayObject)即可实现所需的结果。一个更好的方法可能是重构一些代码,以更好地适应皮肤提供的视图封装(让子组件触发事件而不是试图访问父对象上的方法...)。这是非常酷的东西,一旦你了解所有的皮肤的内部工作。虽然。

    希望这可以帮助。


    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>
    

    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)
    

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

    "parentDocument.parentDocument"    TestProject (@9d7e0a1)   
    

    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. Has anyone else run into this issue? If so, how did you handleit?
    2. If you have developed a workaround, does your solution depend ona specific SDK version, or does it work for all SDK versions?
    3. Was the change in behavior from 4.1 to 4.5 intentional, or isthis a bug? I suspect bug, but I've been unable to find anythingdefinitive one way or the other.

    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.

    解决方案

    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.

    Hope this helps.

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

10-24 13:32