Flex Mobile StageWebView无法显示

时间:2012-11-14 06:21:41

标签: flash flex air flash-builder stagewebview

我在Flex Mobile 4.6(Flash Builder 4.6)上显示StageWebView时遇到问题,当我动态创建它时,事件被正确调用但没有任何可见,这是我的代码:

<components:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
             xmlns:s="library://ns.adobe.com/flex/spark" 
             xmlns:components="spark.components.*" 
             actionBarVisible="false"
             title="ActivityView"
             creationComplete="init()"
             >
<components:layout>
    <s:BasicLayout/>
</components:layout>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
    <![CDATA[
        private var webView:StageWebView;

        protected function init():void {
            webView = new StageWebView();
            webView.stage = this.stage;
            webView.viewPort = new Rectangle(5, 20, screen.width-10, screen.height-40);
            webView.addEventListener(Event.COMPLETE, onURLLoadComplete);
            webView.addEventListener(LocationChangeEvent.LOCATION_CHANGE, onLocationChange);
            webView.loadURL("http://google.com");
        }

        protected function onURLLoadComplete(event:Event):void
        {
            trace("Load Complete");
        }

        protected function onLocationChange(event:LocationChangeEvent):void
        {
            trace("Location change: " + event.location);
        }

    ]]>
</fx:Script>
</components:View>

5 个答案:

答案 0 :(得分:0)

webView.stage = this.stage;

当它在creationComplete事件处理程序中时,this.stage仍为空。

您应该将init()放在addedToStage事件处理程序中。

答案 1 :(得分:0)

您可以使用FlexGlobals.topLevelApplication.stage,因此:

webView.stage = FlexGlobals.topLevelApplication.stage;

答案 2 :(得分:0)

我认为添加StageWebView最安全的方法是在createdChildren()中添加StageWebView(您必须手动定义Web视图的宽度和高度,因为父视图的宽度和高度将为0)或者在viewActiveEvent状态期间。 / p>

Judah编写了一个非常好的WebView类,它更容易使用http://www.judahfrangipane.com/blog/2011/01/16/stagewebview-uicomponent/

希望这有帮助

答案 3 :(得分:0)

您可以使用StageWebViewBridge显示HTML和Javascript通信。我希望这可以帮助您在flex Web和AIR应用程序中显示HTML。在您的代码中,StageWebView没有设置stage,您只需在addedToStage处理程序中分配webView.stage = this.stage或使用以下方法加载html。

        private var webView:StageWebViewBridge;

        protected function addedToStageHandler(event:Event):void
        {
            StageWebViewDisk.addEventListener(StageWebviewDiskEvent.END_DISK_PARSING, onInit );
            StageWebViewDisk.setDebugMode( true );
            StageWebViewDisk.initialize(stage);
        }

        private function onInit(e:StageWebviewDiskEvent):void
        {
            if(!webView)
                webView = new StageWebViewBridge(0, 300, 900, 300);
            webView.addEventListener(StageWebViewBridgeEvent.DEVICE_READY, onDeviceReady );
            webView.addEventListener(Event.COMPLETE, onCompleteHandler);
            webView.addEventListener(ErrorEvent.ERROR, onErrorHandler);
            webView.addEventListener(LocationChangeEvent.LOCATION_CHANGING, onLocationChange);
            var _view:SpriteVisualElement = new SpriteVisualElement();
            _view.addChild(webView);
            addElement(_view);
            webView.loadURL("http://www.google.com");
        }

答案 4 :(得分:0)

你可以添加这样的舞台......

private var _stage : Stage;

private function added_to_stage_handler():void
{
     _stage = this.stage;
     initStage();
}
private function initStage() : void
{
     webView.stage = _stage;
}
相关问题