简体   繁体   English

Adobe Air - 用空气打开文件

[英]Adobe Air - Opening a File With Air

So I've created an Air app that saves to a custom file type. 所以我创建了一个Air应用程序,可以保存为自定义文件类型。 I've set up the file associations when I publish the app and, when you double click the file it opens the air app. 我在发布应用程序时设置了文件关联,当您双击该文件时,它会打开空中应用程序。 What are the hooks for me to detect that the app has been opened via a file? 有什么钩子可以检测到应用程序是通过文件打开的? Obviously, I need to detect this and then get the app to open the file itself. 显然,我需要检测到这一点,然后让应用程序打开文件本身。

Listen for the invoke event on the WindowedApplication or its nativeApplication . WindowedApplication或其nativeApplication上侦听invoke事件。 It has an arguments array property that holds the string arguments passed during this invocation. 它有一个arguments数组属性,用于保存在此​​调用期间传递的字符串参数。

The NativeApplication object of an AIR application dispatches an invoke event when the application is invoked. invoke应用程序时,AIR应用程序的NativeApplication对象将调度invoke事件。

The NativeApplication object always dispatches an invoke event when an application is launched, but the event may be dispatched at other times as well. NativeApplication对象始终在启动应用程序时调度invoke事件,但也可以在其他时间调度该事件。 For example, a running application dispatches an additional InvokeEvent when a user activates a file associated with the application. 例如,当用户激活与应用程序关联的文件时,正在运行的应用程序将调度其他InvokeEvent

Only a single instance of a particular application can be launched. 只能启动特定应用程序的单个实例。 Subsequent attempts to launch the application will result in a new invoke event dispatched by the NativeApplication object of the running instance. 后续尝试启动应用程序将导致正在运行的实例的NativeApplication对象调度的新调用事件。 It is an application's responsibility to handle this event and take the appropriate action, such as opening a new application window to display the data in a file. 应用程序负责处理此事件并采取适当的操作,例如打开新的应用程序窗口以在文件中显示数据。

InvokeEvent objects are dispatched by the NativeApplication object ( NativeApplication.nativeApplication ). InvokeEvent对象由NativeApplication对象( NativeApplication.nativeApplication )调度。 To receive invoke events, call the addEventListener() method of the NativeApplication object. 要接收调用事件,请调用NativeApplication对象的addEventListener()方法。 When an event listener registers for an invoke event, it will also receive all invoke events that occurred before the registration. 当事件侦听器注册invoke事件时,它还将接收在注册之前发生的所有invoke事件。 These earlier events are dispatched after the call to addEventListener() returns, but not necessarily before a new invoke event that might be might be dispatched after registration. 调用addEventListener()后,将调度这些早期事件,但不一定在注册后可能调度的新调用事件之前调度。 Thus, you should not rely on dispatch order. 因此,您不应该依赖派遣订单。

<mx:WindowedApplication creationComplete="init()">
  <mx:Script>
  <![CDATA[
     public function init():void
     {
          NativeApplication.nativeApplication.addEventListener(InvokeEvent.Invoke, onInvoke);
     }
     public function onInvoke(e:InvokeEvent):void
     {
          var args:Array = e.arguments;
          trace("There are " + args.length + " arguments");
          for(var i:int = 0; i < args.length; i++)
          {
               trace("Argument #" + i + " " + args[i]);
          }
     }
  ]]>
  </mx:Script>
</mx:WindowedApplication>

Listen to the InvokeEvent which will hold into the arguments property the file name requested: 收听InvokeEvent ,它将在arguments属性中保存所请求的文件名:

Quick mxml example: 快速mxml示例:

<?xml version="1.0"?>
<mx:WindowedApplication
    xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:s="spark.components.*"
    invoke="onAppInvoke(event);">
    <mx:Script><![CDATA[
      import mx.controls.Alert;

      private function onAppInvoke(event:InvokeEvent):void {
        if (event.arguments.length>0) {
           // ok app call with an arguments
           var fileName:String=event.arguments[0];
           Alert.show("app open with : "+fileName);
        } else {
           // app open normally
           Alert.show("normal launch");
        }
      }
     ]]></mx:Script>
</mx:WindowedApplication>

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

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