2

I've build a native extension for my project to fire two event about login and logout,this is code (NdPlatformExtension.as), the extIntance for singleton have tried but get same result,so ignore it.

private static const API_LOGIN_CALL:String = "LogIn";
private static const API_LOGOUT_CALL:String = "LogOut";
private static const EXTENSION_ID:String = "NdPlatformExt";     

public var extContext:ExtensionContext; 

private static var extIntance:NdPlatformExtension = null; 

public function NdPlatformExtension()
{           
  extContext = ExtensionContext.createExtensionContext(EXTENSION_ID,"");        
  extContext.addEventListener(StatusEvent.STATUS,onEvent);
}

public static function get ExtInstance():NdPlatformExtension {
  if(extIntance == null) {
    extIntance = new NdPlatformExtension(); 
  } 
  return extIntance;
}

public function onEvent(e:StatusEvent):void { 
  ConsoleLog(e.code);
  switch(e.code)            
  {             
    case NdPlatformEvent.EVENT_API_LOGIN_RESULT:
    {
      ConsoleLog("event dispatch start");                       
      var _succed:Boolean = dispatchEvent(new NdPlatformEvent(NdPlatformEvent.EVENT_API_LOGIN_RESULT,e.level.split('|')));
      _succed = dispatchEvent(new NdPlatformEvent(NdPlatformEvent.EVENT_API_TEST,null));
      ConsoleLog(_succed);//i've been checked on the log the _succed is true
    }
    break;  
    default:
    {
      dispatchEvent(new NdPlatformEvent(NdPlatformEvent.EVENT_API_LOGIN_RESULT,null));
    }
    break;
  }                 
} 

public function LogIn():String {
  var _result:String = ""; 
  try
  {
    _result = extContext.call(API_LOGIN_CALL) as String;
  }
  catch(_e:Error)
  {
    _result = _e.message;               
  }
  return _result;   
}   

public function LogOut():String {
  var _result:String = ""; 
  try
  {
    _result = extContext.call(API_LOGOUT_CALL) as String;
  }
  catch(_e:Error)
  {
    _result = _e.message;               
  }
  return _result;   
}
}
}

And build a test flex project to listen the custom events, but all listeners I added are not fire. Here the view file content,the listener will add in view creationComplete.

...creationComplete="init()" title="Test">
  <fx:Script>
    <![CDATA[
        import NdPlatformExt.NdPlatformEvent;
        import NdPlatformExt.NdPlatformExtension;

        import flash.events.Event;

        private var ext:NdPlatformExtension = new NdPlatformExtension();


        protected function button1_clickHandler(event:MouseEvent):void

        {       
            ext.LogIn();    
        }   

        protected function button2_clickHandler(event:MouseEvent):void

        {           

        }           

        public function init():void
        {
            ext.addEventListener(NdPlatformEvent.EVENT_API_LOGIN_RESULT,onLoginResult,true);    
            ext.addEventListener(NdPlatformEvent.EVENT_API_TEST,onTest,true);
        }

        public function onLoginResult(_event:NdPlatformEvent):void{
            ext.ConsoleLog("event receive ok");
            ext.LogOut();
        }


        public function onTest():void{
            ext.ConsoleLog("test receive ok");
        }

    ]]>

</fx:Script>
<fx:Declarations>
</fx:Declarations>
<s:Label id="output" x="14" y="70" width="300" height="32" text=""/>
<s:Button x="44" y="19" width="231" label="Login"
          click="button1_clickHandler(event)" alignmentBaseline="ideographicCenter"/>

<s:Button x="44" y="111" width="231" label="Purchase"
          click="button2_clickHandler(event)" alignmentBaseline="ideographicCenter"/>

but all listener i added are not fire.

4

0 回答 0