在flex NativeProcess中传递参数

时间:2012-04-05 07:47:34

标签: actionscript-3 flex air flex4

我想将两个参数传递给nativeProcess。

当我使用带命令的windows命令运行exe文件时,它正在运行。 窗口命令是“abc.exe a.txt b.txt”

如何使用flex nativeProcess将两个参数传递给该格式的exe?

这是我到目前为止所做的:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="windowedapplication1_applicationCompleteHandler(event)">
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            private var process:NativeProcess;
            protected function windowedapplication1_applicationCompleteHandler(event:FlexEvent):void
            {
                if (NativeProcess.isSupported)
                {
                    callExe();
                }
                else
                {
                    textReceived.text = "NativeProcess not supported.";
                }
            }

            public function callExe():void
            {
                var a_FilePath:String = File.applicationStorageDirectory.resolvePath("dist/a.txt").nativePath;
                var b_FilePath:File = File.applicationStorageDirectory.resolvePath("dist/b.txt").nativePath;

                if (Capabilities.os.toLowerCase().indexOf("win") > -1)
                {
                    var file:File = File.applicationStorageDirectory.resolvePath("dist/abc.exe");
                }

                var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();

//我把这行放在下面,它在我的情况下起作用

nativeProcessStartupInfo.workingDirectory = File.applicationStorageDirectory.resolvePath();

nativeProcessStartupInfo.executable = file;

                var args:Vector.<String> = new Vector.<String>();
                args.push(a_FilePath);
                args.push(b_FilePath);

                nativeProcessStartupInfo.arguments = args;

                process = new NativeProcess();
                process.start(nativeProcessStartupInfo);
                process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
                process.addEventListener(ProgressEvent.STANDARD_INPUT_PROGRESS, inputProgressListener);
                process.addEventListener(ProgressEvent.PROGRESS, progressEvent);
                process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, errorData);
            }

            public function inputProgressListener(event:ProgressEvent):void
            {
                textReceived.text += "Input Progress Event";
            }
            public function onOutputData(event:ProgressEvent):void
            {
                textReceived.text += "Output Event";
            }
            public function progressEvent(event:ProgressEvent):void
            {
                textReceived.text += "Progress Event";  
            }
            public function errorData(event:ProgressEvent):void
            {
                textReceived.text += "Error Event";
            }
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <mx:TextInput id="textReceived" width="352" height="200"/>

</s:WindowedApplication>

1 个答案:

答案 0 :(得分:2)

我通过以粗体添加以下行解决了我的难题:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="windowedapplication1_applicationCompleteHandler(event)">
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            private var process:NativeProcess;
            protected function windowedapplication1_applicationCompleteHandler(event:FlexEvent):void
            {
                if (NativeProcess.isSupported)
                {
                    callExe();
                }
                else
                {
                    textReceived.text = "NativeProcess not supported.";
                }
            }

            public function callExe():void
            {
                var a_FilePath:String = File.applicationStorageDirectory.resolvePath("dist/a.txt").nativePath;
                var b_FilePath:File = File.applicationStorageDirectory.resolvePath("dist/b.txt").nativePath;

                if (Capabilities.os.toLowerCase().indexOf("win") > -1)
                {
                    var file:File = File.applicationStorageDirectory.resolvePath("dist/abc.exe");
                }

                var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();

//我把这行放在下面,它在我的情况下起作用

nativeProcessStartupInfo.workingDirectory = File.applicationStorageDirectory.resolvePath();

nativeProcessStartupInfo.executable = file;

                var args:Vector.<String> = new Vector.<String>();
                args.push(a_FilePath);
                args.push(b_FilePath);

                nativeProcessStartupInfo.arguments = args;

                process = new NativeProcess();
                process.start(nativeProcessStartupInfo);
                process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
                process.addEventListener(ProgressEvent.STANDARD_INPUT_PROGRESS, inputProgressListener);
                process.addEventListener(ProgressEvent.PROGRESS, progressEvent);
                process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, errorData);
            }

            public function inputProgressListener(event:ProgressEvent):void
            {
                textReceived.text += "Input Progress Event";
            }
            public function onOutputData(event:ProgressEvent):void
            {
                textReceived.text += "Output Event";
            }
            public function progressEvent(event:ProgressEvent):void
            {
                textReceived.text += "Progress Event";  
            }
            public function errorData(event:ProgressEvent):void
            {
                textReceived.text += "Error Event";
            }
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <mx:TextInput id="textReceived" width="352" height="200"/>

</s:WindowedApplication>